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 int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext);
65 const int pt_mega = 1024 * 1024;
66 //-----------------------------------------------------------------------------
71 //-------------------------------------------------------------------------
72 BAK_FILE(const string & fileName, bool removeBak)
77 BAK_FILE::removeBak = removeBak;
78 fileNameBak = fileName + ".bak";
79 if (rename(fileName.c_str(), fileNameBak.c_str()))
81 printfd(__FILE__, "BAK_FILE::BAK_FILE - rename failed. Message: '%s'\n", strerror(errno));
89 //-------------------------------------------------------------------------
92 if(bakSuccessed && removeBak)
94 if (unlink(fileNameBak.c_str()))
96 printfd(__FILE__, "BAK_FILE::~BAK_FILE - unlink failed. Message: '%s'\n", strerror(errno));
100 //-------------------------------------------------------------------------
108 //-----------------------------------------------------------------------------
109 class FILES_STORE_CREATOR
115 FILES_STORE_CREATOR()
116 : fs(new FILES_STORE())
119 ~FILES_STORE_CREATOR()
124 FILES_STORE * GetStore()
129 //-----------------------------------------------------------------------------
130 //-----------------------------------------------------------------------------
131 //-----------------------------------------------------------------------------
132 FILES_STORE_CREATOR fsc;
133 //-----------------------------------------------------------------------------
134 //-----------------------------------------------------------------------------
135 //-----------------------------------------------------------------------------
136 BASE_STORE * GetStore()
138 return fsc.GetStore();
140 //-----------------------------------------------------------------------------
141 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
147 //-----------------------------------------------------------------------------
148 FILES_STORE_SETTINGS::~FILES_STORE_SETTINGS()
151 //-----------------------------------------------------------------------------
152 int FILES_STORE_SETTINGS::ParseOwner(const vector<PARAM_VALUE> & moduleParams, const string & owner, uid_t * uid)
156 vector<PARAM_VALUE>::const_iterator pvi;
157 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
158 if (pvi == moduleParams.end())
160 errorStr = "Parameter \'" + owner + "\' not found.";
161 printfd(__FILE__, "%s\n", errorStr.c_str());
164 if (User2UID(pvi->value[0].c_str(), uid) < 0)
166 errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'";
167 printfd(__FILE__, "%s\n", errorStr.c_str());
172 //-----------------------------------------------------------------------------
173 int FILES_STORE_SETTINGS::ParseGroup(const vector<PARAM_VALUE> & moduleParams, const string & group, gid_t * gid)
177 vector<PARAM_VALUE>::const_iterator pvi;
178 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
179 if (pvi == moduleParams.end())
181 errorStr = "Parameter \'" + group + "\' not found.";
182 printfd(__FILE__, "%s\n", errorStr.c_str());
185 if (Group2GID(pvi->value[0].c_str(), gid) < 0)
187 errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'";
188 printfd(__FILE__, "%s\n", errorStr.c_str());
193 //-----------------------------------------------------------------------------
194 int FILES_STORE_SETTINGS::ParseYesNo(const string & value, bool * val)
196 if (0 == strcasecmp(value.c_str(), "yes"))
201 if (0 == strcasecmp(value.c_str(), "no"))
207 errorStr = "Incorrect value \'" + value + "\'.";
210 //-----------------------------------------------------------------------------
211 int FILES_STORE_SETTINGS::ParseMode(const vector<PARAM_VALUE> & moduleParams, const string & modeStr, mode_t * mode)
215 vector<PARAM_VALUE>::const_iterator pvi;
216 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
217 if (pvi == moduleParams.end())
219 errorStr = "Parameter \'" + modeStr + "\' not found.";
220 printfd(__FILE__, "%s\n", errorStr.c_str());
223 if (Str2Mode(pvi->value[0].c_str(), mode) < 0)
225 errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'";
226 printfd(__FILE__, "%s\n", errorStr.c_str());
231 //-----------------------------------------------------------------------------
232 int FILES_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
234 if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0)
236 if (ParseGroup(s.moduleParams, "StatGroup", &statGID) < 0)
238 if (ParseMode(s.moduleParams, "StatMode", &statMode) < 0)
241 if (ParseOwner(s.moduleParams, "ConfOwner", &confUID) < 0)
243 if (ParseGroup(s.moduleParams, "ConfGroup", &confGID) < 0)
245 if (ParseMode(s.moduleParams, "ConfMode", &confMode) < 0)
248 if (ParseOwner(s.moduleParams, "UserLogOwner", &userLogUID) < 0)
250 if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0)
252 if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0)
255 vector<PARAM_VALUE>::const_iterator pvi;
257 pv.param = "RemoveBak";
258 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
259 if (pvi == s.moduleParams.end())
265 if (ParseYesNo(pvi->value[0], &removeBak))
267 printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n");
272 pv.param = "ReadBak";
273 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
274 if (pvi == s.moduleParams.end())
280 if (ParseYesNo(pvi->value[0], &readBak))
282 printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n");
287 pv.param = "WorkDir";
288 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
289 if (pvi == s.moduleParams.end())
291 errorStr = "Parameter \'WorkDir\' not found.";
292 printfd(__FILE__, "Parameter 'WorkDir' not found\n");
296 workDir = pvi->value[0];
297 if (workDir.size() && workDir[workDir.size() - 1] == '/')
299 workDir.resize(workDir.size() - 1);
301 usersDir = workDir + "/users/";
302 tariffsDir = workDir + "/tariffs/";
303 adminsDir = workDir + "/admins/";
307 //-----------------------------------------------------------------------------
308 const string & FILES_STORE_SETTINGS::GetStrError() const
312 //-----------------------------------------------------------------------------
313 int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid)
319 errorStr = string("User \'") + string(user) + string("\' not found in system.");
320 printfd(__FILE__, "%s\n", errorStr.c_str());
327 //-----------------------------------------------------------------------------
328 int FILES_STORE_SETTINGS::Group2GID(const char * gr, gid_t * gid)
334 errorStr = string("Group \'") + string(gr) + string("\' not found in system.");
335 printfd(__FILE__, "%s\n", errorStr.c_str());
342 //-----------------------------------------------------------------------------
343 int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode)
350 errorStr = string("Error parsing mode \'") + str + string("\'");
351 printfd(__FILE__, "%s\n", errorStr.c_str());
355 for (int i = 0; i < 3; i++)
356 if (str[i] > '7' || str[i] < '0')
358 errorStr = string("Error parsing mode \'") + str + string("\'");
359 printfd(__FILE__, "%s\n", errorStr.c_str());
367 *mode = ((mode_t)c) + ((mode_t)b << 3) + ((mode_t)a << 6);
371 //-----------------------------------------------------------------------------
372 string FILES_STORE_SETTINGS::GetWorkDir() const
376 //-----------------------------------------------------------------------------
377 string FILES_STORE_SETTINGS::GetUsersDir() const
381 //-----------------------------------------------------------------------------
382 string FILES_STORE_SETTINGS::GetAdminsDir() const
386 //-----------------------------------------------------------------------------
387 string FILES_STORE_SETTINGS::GetTariffsDir() const
391 //-----------------------------------------------------------------------------
392 mode_t FILES_STORE_SETTINGS::GetStatMode() const
396 //-----------------------------------------------------------------------------
397 mode_t FILES_STORE_SETTINGS::GetStatModeDir() const
399 mode_t mode = statMode;
400 if (statMode & S_IRUSR) mode |= S_IXUSR;
401 if (statMode & S_IRGRP) mode |= S_IXGRP;
402 if (statMode & S_IROTH) mode |= S_IXOTH;
405 //-----------------------------------------------------------------------------
406 uid_t FILES_STORE_SETTINGS::GetStatUID() const
410 //-----------------------------------------------------------------------------
411 gid_t FILES_STORE_SETTINGS::GetStatGID() const
415 //-----------------------------------------------------------------------------
416 mode_t FILES_STORE_SETTINGS::GetConfMode() const
420 //-----------------------------------------------------------------------------
421 mode_t FILES_STORE_SETTINGS::GetConfModeDir() const
423 mode_t mode = confMode;
424 if (confMode & S_IRUSR) mode |= S_IXUSR;
425 if (confMode & S_IRGRP) mode |= S_IXGRP;
426 if (confMode & S_IROTH) mode |= S_IXOTH;
429 //-----------------------------------------------------------------------------
430 uid_t FILES_STORE_SETTINGS::GetConfUID() const
434 //-----------------------------------------------------------------------------
435 gid_t FILES_STORE_SETTINGS::GetConfGID() const
439 //-----------------------------------------------------------------------------
440 mode_t FILES_STORE_SETTINGS::GetLogMode() const
444 //-----------------------------------------------------------------------------
445 uid_t FILES_STORE_SETTINGS::GetLogUID() const
449 //-----------------------------------------------------------------------------
450 gid_t FILES_STORE_SETTINGS::GetLogGID() const
454 //-----------------------------------------------------------------------------
455 bool FILES_STORE_SETTINGS::GetRemoveBak() const
459 //-----------------------------------------------------------------------------
460 bool FILES_STORE_SETTINGS::GetReadBak() const
464 //-----------------------------------------------------------------------------
465 //-----------------------------------------------------------------------------
466 //-----------------------------------------------------------------------------
467 FILES_STORE::FILES_STORE()
469 version = "file_store v.1.04";
471 pthread_mutexattr_t attr;
472 pthread_mutexattr_init(&attr);
473 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
474 pthread_mutex_init(&mutex, &attr);
476 //-----------------------------------------------------------------------------
477 FILES_STORE::~FILES_STORE()
481 //-----------------------------------------------------------------------------
482 void FILES_STORE::SetSettings(const MODULE_SETTINGS & s)
486 //-----------------------------------------------------------------------------
487 int FILES_STORE::ParseSettings()
489 int ret = storeSettings.ParseSettings(settings);
492 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
493 errorStr = storeSettings.GetStrError();
497 //-----------------------------------------------------------------------------
498 const string & FILES_STORE::GetStrError() const
500 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
503 //-----------------------------------------------------------------------------
504 const string & FILES_STORE::GetVersion() const
508 //-----------------------------------------------------------------------------
509 int FILES_STORE::GetUsersList(vector<string> * userList) const
511 vector<string> files;
513 if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, ""))
515 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
516 errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + string(strerror(errno));
520 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
522 userList->swap(files);
526 //-----------------------------------------------------------------------------
527 int FILES_STORE::GetAdminsList(vector<string> * adminList) const
529 vector<string> files;
531 if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm"))
533 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
534 errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + string(strerror(errno));
538 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
540 adminList->swap(files);
544 //-----------------------------------------------------------------------------
545 int FILES_STORE::GetTariffsList(vector<string> * tariffList) const
547 vector<string> files;
549 if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf"))
551 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
552 errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + string(strerror(errno));
556 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
558 tariffList->swap(files);
562 //-----------------------------------------------------------------------------
563 int FILES_STORE::RemoveDir(const char * path) const
565 vector<string> fileList;
567 GetFileList(&fileList, path, S_IFREG, "");
569 for (unsigned i = 0; i < fileList.size(); i++)
571 string file = path + string("/") + fileList[i];
572 if (unlink(file.c_str()))
574 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
575 errorStr = "unlink failed. Message: '";
576 errorStr += strerror(errno);
578 printfd(__FILE__, "FILES_STORE::RemoveDir - unlink failed. Message: '%s'\n", strerror(errno));
584 GetFileList(&fileList, path, S_IFDIR, "");
586 for (unsigned i = 0; i < fileList.size(); i++)
588 string dir = string(path) + "/" + fileList[i];
589 RemoveDir(dir.c_str());
594 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
595 errorStr = "rmdir failed. Message: '";
596 errorStr += strerror(errno);
598 printfd(__FILE__, "FILES_STORE::RemoveDir - rmdir failed. Message: '%s'\n", strerror(errno));
604 //-----------------------------------------------------------------------------
605 int FILES_STORE::AddUser(const string & login) const
609 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
611 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
613 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
614 errorStr = string("mkdir failed. Message: '") + strerror(errno) + "'";
615 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
619 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
622 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
623 errorStr = "Cannot create file \"" + fileName + "\'";
624 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
627 /*f = fopen(fileName.c_str(), "wt");
630 if (fprintf(f, "\n") < 0)
632 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
633 errorStr = "fprintf failed. Message: '";
634 errorStr += strerror(errno);
636 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
643 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
644 errorStr = "Cannot create file \"" + fileName + "\'";
645 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
649 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
652 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
653 errorStr = "Cannot create file \"" + fileName + "\'";
654 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
657 /*f = fopen(fileName.c_str(), "wt");
660 if (fprintf(f, "\n") < 0)
662 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
663 errorStr = "fprintf failed. Message: '";
664 errorStr += strerror(errno);
666 printfd(__FILE__, "FILES_STORE::AddUser - fprintf failed. Message: '%s'\n", strerror(errno));
673 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
674 errorStr = "Cannot create file \"" + fileName + "\'";
675 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
680 //-----------------------------------------------------------------------------
681 int FILES_STORE::DelUser(const string & login) const
686 strprintf(&dirName, "%s/"DELETED_USERS_DIR, storeSettings.GetWorkDir().c_str());
687 if (access(dirName.c_str(), F_OK) != 0)
689 if (mkdir(dirName.c_str(), 0700) != 0)
691 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
692 errorStr = "Directory '" + dirName + "' cannot be created.";
693 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
698 if (access(dirName.c_str(), F_OK) == 0)
700 strprintf(&dirName, "%s/"DELETED_USERS_DIR"/%s.%lu", storeSettings.GetWorkDir().c_str(), login.c_str(), time(NULL));
701 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
702 if (rename(dirName1.c_str(), dirName.c_str()))
704 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
705 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
706 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
712 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
713 if (RemoveDir(dirName.c_str()))
720 //-----------------------------------------------------------------------------
721 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login) const
724 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
725 if (RestoreUserConf(conf, login, fileName))
727 if (!storeSettings.GetReadBak())
731 return RestoreUserConf(conf, login, fileName + ".bak");
735 //-----------------------------------------------------------------------------
736 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const string & login, const string & fileName) const
738 CONFIGFILE cf(fileName);
744 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
745 errorStr = "User \'" + login + "\' data not read.";
746 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
750 if (cf.ReadString("Password", &conf->password, "") < 0)
752 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
753 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
754 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
757 if (conf->password.empty())
759 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
760 errorStr = "User \'" + login + "\' password is blank.";
761 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
765 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
767 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
768 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
769 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
772 if (conf->tariffName.empty())
774 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
775 errorStr = "User \'" + login + "\' tariff is blank.";
776 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
781 cf.ReadString("IP", &ipStr, "?");
789 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
790 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
791 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
796 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
798 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
799 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
800 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
804 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
806 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
807 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
808 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
812 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
814 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
815 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
816 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
820 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
821 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
822 cf.ReadString("TariffChange", &conf->nextTariff, "");
823 cf.ReadString("Group", &conf->group, "");
824 cf.ReadString("RealName", &conf->realName, "");
825 cf.ReadString("Address", &conf->address, "");
826 cf.ReadString("Phone", &conf->phone, "");
827 cf.ReadString("Note", &conf->note, "");
828 cf.ReadString("email", &conf->email, "");
830 char userdataName[12];
831 for (int i = 0; i < USERDATA_NUM; i++)
833 snprintf(userdataName, 12, "Userdata%d", i);
834 cf.ReadString(userdataName, &conf->userdata[i], "");
837 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
839 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
840 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
841 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
847 //-----------------------------------------------------------------------------
848 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login) const
851 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
853 if (RestoreUserStat(stat, login, fileName))
855 if (!storeSettings.GetReadBak())
859 return RestoreUserStat(stat, login, fileName + ".bak");
863 //-----------------------------------------------------------------------------
864 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const string & login, const string & fileName) const
866 CONFIGFILE cf(fileName);
872 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
873 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
874 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
880 for (int i = 0; i < DIR_NUM; i++)
883 snprintf(s, 22, "D%d", i);
884 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
886 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
887 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
888 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
891 stat->down[i] = traff;
893 snprintf(s, 22, "U%d", i);
894 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
896 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
897 errorStr = "User \'" + login + "\' stat not read. Parameter " + string(s);
898 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
904 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
906 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
907 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
908 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
912 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
914 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
915 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
916 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
920 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
922 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
923 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
924 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
928 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
930 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
931 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
932 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
936 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
938 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
939 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
940 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
944 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
946 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
947 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
948 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
954 //-----------------------------------------------------------------------------
955 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const string & login) const
958 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
960 //BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
962 CONFIGFILE cfstat(fileName, true);
964 int e = cfstat.Error();
968 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
969 errorStr = string("User \'") + login + "\' conf not written\n";
970 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
974 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
975 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
979 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
980 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
983 cfstat.WriteString("Password", conf.password);
984 cfstat.WriteInt ("Passive", conf.passive);
985 cfstat.WriteInt ("Down", conf.disabled);
986 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
987 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
988 cfstat.WriteString("Tariff", conf.tariffName);
989 cfstat.WriteString("Address", conf.address);
990 cfstat.WriteString("Phone", conf.phone);
991 cfstat.WriteString("Email", conf.email);
992 cfstat.WriteString("Note", conf.note);
993 cfstat.WriteString("RealName", conf.realName);
994 cfstat.WriteString("Group", conf.group);
995 cfstat.WriteDouble("Credit", conf.credit);
996 cfstat.WriteString("TariffChange", conf.nextTariff);
998 char userdataName[12];
999 for (int i = 0; i < USERDATA_NUM; i++)
1001 snprintf(userdataName, 12, "Userdata%d", i);
1002 cfstat.WriteString(userdataName, conf.userdata[i]);
1004 cfstat.WriteInt("CreditExpire", conf.creditExpire);
1008 cfstat.WriteString("IP", ipStr.str());
1012 //-----------------------------------------------------------------------------
1013 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const string & login) const
1017 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
1019 //BAK_FILE bakFile(fileName, storeSettings.GetRemoveBak());
1022 CONFIGFILE cfstat(fileName, true);
1023 int e = cfstat.Error();
1027 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1028 errorStr = string("User \'") + login + "\' stat not written\n";
1029 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
1033 for (int i = 0; i < DIR_NUM; i++)
1035 snprintf(s, 22, "D%d", i);
1036 cfstat.WriteInt(s, stat.down[i]);
1037 snprintf(s, 22, "U%d", i);
1038 cfstat.WriteInt(s, stat.up[i]);
1041 cfstat.WriteDouble("Cash", stat.cash);
1042 cfstat.WriteDouble("FreeMb", stat.freeMb);
1043 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
1044 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1045 cfstat.WriteInt("PassiveTime", stat.passiveTime);
1046 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
1049 int e = chmod(fileName.c_str(), storeSettings.GetStatMode());
1050 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
1054 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1055 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1060 //-----------------------------------------------------------------------------
1061 int FILES_STORE::WriteLogString(const string & str, const string & login) const
1064 time_t tm = time(NULL);
1066 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
1067 f = fopen(fileName.c_str(), "at");
1071 fprintf(f, "%s", LogDate(tm));
1073 fprintf(f, "%s", str.c_str());
1079 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1080 errorStr = "Cannot open \'" + fileName + "\'";
1081 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1085 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1086 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1090 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1091 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1096 //-----------------------------------------------------------------------------
1097 int FILES_STORE::WriteLog2String(const string & str, const string & login) const
1100 time_t tm = time(NULL);
1102 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
1103 f = fopen(fileName.c_str(), "at");
1107 fprintf(f, "%s", LogDate(tm));
1109 fprintf(f, "%s", str.c_str());
1115 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1116 errorStr = "Cannot open \'" + fileName + "\'";
1117 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
1121 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
1122 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
1126 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1127 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1132 //-----------------------------------------------------------------------------
1133 int FILES_STORE::WriteUserChgLog(const string & login,
1134 const string & admLogin,
1136 const string & paramName,
1137 const string & oldValue,
1138 const string & newValue,
1139 const string & message) const
1141 string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
1142 + paramName + "\' parameter changed from \'" + oldValue +
1143 "\' to \'" + newValue + "\'. " + message;
1145 return WriteLogString(userLogMsg, login);
1147 //-----------------------------------------------------------------------------
1148 int FILES_STORE::WriteUserConnect(const string & login, uint32_t ip) const
1150 string logStr = "Connect, " + inet_ntostring(ip);
1151 if (WriteLogString(logStr, login))
1153 return WriteLog2String(logStr, login);
1155 //-----------------------------------------------------------------------------
1156 int FILES_STORE::WriteUserDisconnect(const string & login,
1157 const DIR_TRAFF & up,
1158 const DIR_TRAFF & down,
1159 const DIR_TRAFF & sessionUp,
1160 const DIR_TRAFF & sessionDown,
1163 const std::string & reason) const
1165 stringstream logStr;
1166 logStr << "Disconnect, "
1167 << " session upload: \'"
1169 << "\' session download: \'"
1171 << "\' month upload: \'"
1173 << "\' month download: \'"
1179 if (WriteLogString(logStr.str(), login))
1182 logStr << " freeMb: \'"
1189 return WriteLog2String(logStr.str(), login);
1191 //-----------------------------------------------------------------------------
1192 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const string & login) const
1197 strprintf(&str,"%s/%s/stat.%d.%02d",
1198 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1200 CONFIGFILE s(str, true);
1205 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1206 errorStr = "Cannot create file " + str;
1207 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1213 for (int i = 0; i < DIR_NUM; i++)
1215 snprintf(dirName, 3, "U%d", i);
1216 s.WriteInt(dirName, stat.up[i]);
1217 snprintf(dirName, 3, "D%d", i);
1218 s.WriteInt(dirName, stat.down[i]);
1221 s.WriteDouble("cash", stat.cash);
1225 //-----------------------------------------------------------------------------*/
1226 int FILES_STORE::AddAdmin(const string & login) const
1229 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1231 if (Touch(fileName))
1233 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1234 errorStr = "Cannot create file " + fileName;
1235 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1241 //-----------------------------------------------------------------------------*/
1242 int FILES_STORE::DelAdmin(const string & login) const
1245 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1246 if (unlink(fileName.c_str()))
1248 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1249 errorStr = "unlink failed. Message: '";
1250 errorStr += strerror(errno);
1252 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1256 //-----------------------------------------------------------------------------*/
1257 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1259 char passwordE[2 * ADM_PASSWD_LEN + 2];
1260 char pass[ADM_PASSWD_LEN + 1];
1261 char adminPass[ADM_PASSWD_LEN + 1];
1265 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1268 CONFIGFILE cf(fileName, true);
1274 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1275 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1276 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1280 memset(pass, 0, sizeof(pass));
1281 memset(adminPass, 0, sizeof(adminPass));
1284 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1286 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1287 adminPass[ADM_PASSWD_LEN - 1] = 0;
1289 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1291 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1294 pass[ADM_PASSWD_LEN - 1] = 0;
1295 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1297 cf.WriteString("password", passwordE);
1298 cf.WriteInt("ChgConf", ac.priv.userConf);
1299 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1300 cf.WriteInt("ChgStat", ac.priv.userStat);
1301 cf.WriteInt("ChgCash", ac.priv.userCash);
1302 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1303 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1304 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1309 //-----------------------------------------------------------------------------
1310 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const string & login) const
1313 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1314 CONFIGFILE cf(fileName);
1315 char pass[ADM_PASSWD_LEN + 1];
1316 char password[ADM_PASSWD_LEN + 1];
1317 char passwordE[2 * ADM_PASSWD_LEN + 2];
1324 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1325 errorStr = "Cannot open " + fileName;
1326 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1332 if (cf.ReadString("password", &p, "*"))
1334 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1335 errorStr = "Error in parameter password";
1336 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1340 memset(passwordE, 0, sizeof(passwordE));
1341 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1343 //printfd(__FILE__, "passwordE %s\n", passwordE);
1345 memset(pass, 0, sizeof(pass));
1347 if (passwordE[0] != 0)
1349 Decode21(pass, passwordE);
1350 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1352 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1354 DecodeString(password + 8*i, pass + 8*i, &ctx);
1362 ac->password = password;
1364 if (cf.ReadInt("ChgConf", &a, 0) == 0)
1365 ac->priv.userConf = a;
1368 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1369 errorStr = "Error in parameter ChgConf";
1370 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1374 if (cf.ReadInt("ChgPassword", &a, 0) == 0)
1375 ac->priv.userPasswd = a;
1378 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1379 errorStr = "Error in parameter ChgPassword";
1380 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1384 if (cf.ReadInt("ChgStat", &a, 0) == 0)
1385 ac->priv.userStat = a;
1388 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1389 errorStr = "Error in parameter ChgStat";
1390 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1394 if (cf.ReadInt("ChgCash", &a, 0) == 0)
1395 ac->priv.userCash = a;
1398 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1399 errorStr = "Error in parameter ChgCash";
1400 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1404 if (cf.ReadInt("UsrAddDel", &a, 0) == 0)
1405 ac->priv.userAddDel = a;
1408 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1409 errorStr = "Error in parameter UsrAddDel";
1410 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1414 if (cf.ReadInt("ChgAdmin", &a, 0) == 0)
1415 ac->priv.adminChg = a;
1418 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1419 errorStr = "Error in parameter ChgAdmin";
1420 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1424 if (cf.ReadInt("ChgTariff", &a, 0) == 0)
1425 ac->priv.tariffChg = a;
1428 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1429 errorStr = "Error in parameter ChgTariff";
1430 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1436 //-----------------------------------------------------------------------------
1437 int FILES_STORE::AddTariff(const string & name) const
1440 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1441 if (Touch(fileName))
1443 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1444 errorStr = "Cannot create file " + fileName;
1445 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1450 //-----------------------------------------------------------------------------
1451 int FILES_STORE::DelTariff(const string & name) const
1454 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1455 if (unlink(fileName.c_str()))
1457 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1458 errorStr = "unlink failed. Message: '";
1459 errorStr += strerror(errno);
1461 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1465 //-----------------------------------------------------------------------------
1466 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const string & tariffName) const
1468 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1469 CONFIGFILE conf(fileName);
1471 td->tariffConf.name = tariffName;
1473 if (conf.Error() != 0)
1475 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1476 errorStr = "Cannot read file " + fileName;
1477 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1482 for (int i = 0; i<DIR_NUM; i++)
1484 strprintf(¶m, "Time%d", i);
1485 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1487 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1488 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1489 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1493 ParseTariffTimeStr(str.c_str(),
1494 td->dirPrice[i].hDay,
1495 td->dirPrice[i].mDay,
1496 td->dirPrice[i].hNight,
1497 td->dirPrice[i].mNight);
1499 strprintf(¶m, "PriceDayA%d", i);
1500 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1502 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1503 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1504 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1507 td->dirPrice[i].priceDayA /= (1024*1024);
1509 strprintf(¶m, "PriceDayB%d", i);
1510 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1512 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1513 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1514 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1517 td->dirPrice[i].priceDayB /= (1024*1024);
1519 strprintf(¶m, "PriceNightA%d", i);
1520 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1522 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1523 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1524 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1527 td->dirPrice[i].priceNightA /= (1024*1024);
1529 strprintf(¶m, "PriceNightB%d", i);
1530 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1532 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1533 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1534 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1537 td->dirPrice[i].priceNightB /= (1024*1024);
1539 strprintf(¶m, "Threshold%d", i);
1540 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1542 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1543 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1544 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1548 strprintf(¶m, "SinglePrice%d", i);
1549 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1551 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1552 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1553 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1557 strprintf(¶m, "NoDiscount%d", i);
1558 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1560 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1561 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1562 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1567 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1569 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1570 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1571 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1575 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1577 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1578 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1579 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1583 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1585 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1586 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1587 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1591 if (conf.ReadString("TraffType", &str, "") < 0)
1593 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1594 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1595 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1599 if (!strcasecmp(str.c_str(), "up"))
1600 td->tariffConf.traffType = TRAFF_UP;
1602 if (!strcasecmp(str.c_str(), "down"))
1603 td->tariffConf.traffType = TRAFF_DOWN;
1605 if (!strcasecmp(str.c_str(), "up+down"))
1606 td->tariffConf.traffType = TRAFF_UP_DOWN;
1608 if (!strcasecmp(str.c_str(), "max"))
1609 td->tariffConf.traffType = TRAFF_MAX;
1612 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1613 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1614 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
1619 //-----------------------------------------------------------------------------
1620 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const string & tariffName) const
1622 string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1625 CONFIGFILE cf(fileName, true);
1631 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1632 errorStr = "Error writing tariff " + tariffName;
1633 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1638 for (int i = 0; i < DIR_NUM; i++)
1640 strprintf(¶m, "PriceDayA%d", i);
1641 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1643 strprintf(¶m, "PriceDayB%d", i);
1644 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1646 strprintf(¶m, "PriceNightA%d", i);
1647 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1649 strprintf(¶m, "PriceNightB%d", i);
1650 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1652 strprintf(¶m, "Threshold%d", i);
1653 cf.WriteInt(param, td.dirPrice[i].threshold);
1656 strprintf(¶m, "Time%d", i);
1658 strprintf(&s, "%0d:%0d-%0d:%0d",
1659 td.dirPrice[i].hDay,
1660 td.dirPrice[i].mDay,
1661 td.dirPrice[i].hNight,
1662 td.dirPrice[i].mNight);
1664 cf.WriteString(param, s);
1666 strprintf(¶m, "NoDiscount%d", i);
1667 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1669 strprintf(¶m, "SinglePrice%d", i);
1670 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1673 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1674 cf.WriteDouble("Fee", td.tariffConf.fee);
1675 cf.WriteDouble("Free", td.tariffConf.free);
1677 switch (td.tariffConf.traffType)
1680 cf.WriteString("TraffType", "up");
1683 cf.WriteString("TraffType", "down");
1686 cf.WriteString("TraffType", "up+down");
1689 cf.WriteString("TraffType", "max");
1696 //-----------------------------------------------------------------------------
1697 int FILES_STORE::WriteDetailedStat(const map<IP_DIR_PAIR, STAT_NODE> & statTree,
1699 const string & login) const
1701 char fn[FN_STR_LEN];
1702 char dn[FN_STR_LEN];
1709 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1710 if (access(dn, F_OK) != 0)
1712 if (mkdir(dn, 0700) != 0)
1714 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1715 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1716 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1721 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1722 e += chmod(dn, storeSettings.GetStatModeDir());
1726 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1727 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1732 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1738 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1739 storeSettings.GetUsersDir().c_str(),
1743 if (access(dn, F_OK) != 0)
1745 if (mkdir(dn, 0700) != 0)
1747 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1748 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1749 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1754 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1755 e += chmod(dn, storeSettings.GetStatModeDir());
1759 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1760 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1763 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1764 storeSettings.GetUsersDir().c_str(),
1767 lt->tm_mon+1 < 10 ? "0" : "",
1769 if (access(dn, F_OK) != 0)
1771 if (mkdir(dn, 0700) != 0)
1773 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1774 errorStr = "Directory \'" + string(dn) + "\' cannot be created.";
1775 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1780 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1781 e += chmod(dn, storeSettings.GetStatModeDir());
1785 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1786 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1789 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1791 statFile = fopen (fn, "at");
1795 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1796 errorStr = "File \'" + string(fn) + "\' cannot be written.";
1797 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1804 lt1 = localtime(&lastStat);
1813 lt2 = localtime(&t);
1819 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1820 h1, m1, s1, h2, m2, s2) < 0)
1822 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1823 errorStr = string("fprint failed. Message: '") + strerror(errno) + "'";
1824 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1829 map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1830 stIter = statTree.begin();
1832 while (stIter != statTree.end())
1835 x2str(stIter->second.up, u);
1836 x2str(stIter->second.down, d);
1837 #ifdef TRAFF_STAT_WITH_PORTS
1838 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1839 inet_ntostring(stIter->first.ip).c_str(),
1844 stIter->second.cash) < 0)
1846 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1847 errorStr = "fprint failed. Message: '";
1848 errorStr += strerror(errno);
1850 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1855 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1856 inet_ntostring(stIter->first.ip).c_str(),
1860 stIter->second.cash) < 0)
1862 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1863 errorStr = string("fprint failed. Message: '");
1864 errorStr += strerror(errno);
1866 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1877 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1878 e += chmod(fn, storeSettings.GetStatMode());
1882 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1883 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1888 //-----------------------------------------------------------------------------
1889 int FILES_STORE::AddMessage(STG_MSG * msg, const string & login) const
1891 //ðÒÏ×ÅÒÉÔØ ÅÓÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1892 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1893 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1899 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1900 if (access(dn.c_str(), F_OK) != 0)
1902 if (mkdir(dn.c_str(), 0700) != 0)
1904 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1905 errorStr = "Directory \'";
1907 errorStr += "\' cannot be created.";
1908 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1913 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1915 gettimeofday(&tv, NULL);
1917 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1918 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1922 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1923 errorStr = "File \'";
1925 errorStr += "\' cannot be writen.";
1926 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1930 return EditMessage(*msg, login);
1932 //-----------------------------------------------------------------------------
1933 int FILES_STORE::EditMessage(const STG_MSG & msg, const string & login) const
1935 //ðÒÏ×ÅÒÉÔØ ÅÓÌÔØ ÌÉ ÄÉÒÅËÔÏÒÉÑ ÄÌÑ ÓÏÏÂÝÅÎÉÊ. åÓÌÉ ÎÅÔ - ÓÏÚÄÁÔØ.
1936 //úÁÔÅÍ ÐÏÌÏÖÉÔØ ÓÏÏÂÝÅÎÉÅ Ó ÉÍÅÎÅÍ ÆÁÊÌÁ - ×ÒÅÍÅÎÎOÊ ÍÅÔËÏÊ. úÁÐÉÓÁÔØ ÔÕÄÁ
1937 //ÔÅËÓÔ É ÐÒÉÏÒÉÔÅÔ.
1942 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1944 if (access(fileName.c_str(), F_OK) != 0)
1947 x2str(msg.header.id, idstr);
1948 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1949 errorStr = "Message for user \'";
1950 errorStr += login + "\' with ID \'";
1951 errorStr += idstr + "\' does not exist.";
1952 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1956 Touch(fileName + ".new");
1958 msgFile = fopen((fileName + ".new").c_str(), "wt");
1961 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1962 errorStr = "File \'" + fileName + "\' cannot be writen.";
1963 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1968 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
1969 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1970 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1971 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1972 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1973 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1974 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1978 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1979 errorStr = string("fprintf failed. Message: '") + strerror(errno) + "'";
1980 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1987 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1989 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1991 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1992 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1993 printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
1999 //-----------------------------------------------------------------------------
2000 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const string & login) const
2003 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2004 msg->header.id = id;
2005 return ReadMessage(fn, &msg->header, &msg->text);
2007 //-----------------------------------------------------------------------------
2008 int FILES_STORE::DelMessage(uint64_t id, const string & login) const
2011 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
2013 return unlink(fn.c_str());
2015 //-----------------------------------------------------------------------------
2016 int FILES_STORE::GetMessageHdrs(vector<STG_MSG_HDR> * hdrsList, const string & login) const
2018 string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
2020 //hdrsList->resize(messages.size());
2022 if (access(dn.c_str(), F_OK) != 0)
2027 vector<string> messages;
2028 GetFileList(&messages, dn, S_IFREG, "");
2030 for (unsigned i = 0; i < messages.size(); i++)
2032 unsigned long long id = 0;
2034 if (str2x(messages[i].c_str(), id))
2036 if (unlink((dn + messages[i]).c_str()))
2038 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2039 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2040 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2047 if (ReadMessage(dn + messages[i], &hdr, NULL))
2054 if (unlink((dn + messages[i]).c_str()))
2056 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2057 errorStr = string("unlink failed. Message: '") + strerror(errno) + "'";
2058 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
2065 hdrsList->push_back(hdr);
2069 //-----------------------------------------------------------------------------
2070 int FILES_STORE::ReadMessage(const string & fileName,
2072 string * text) const
2075 msgFile = fopen(fileName.c_str(), "rt");
2078 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2079 errorStr = "File \'";
2080 errorStr += fileName;
2081 errorStr += "\' cannot be openned.";
2082 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
2088 d[1] = &hdr->lastSendTime;
2089 d[2] = &hdr->creationTime;
2090 d[3] = &hdr->showTime;
2091 d[4] = (unsigned*)(&hdr->repeat);
2092 d[5] = &hdr->repeatPeriod;
2094 memset(p, 0, sizeof(p));
2096 for (int pos = 0; pos < 6; pos++)
2098 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
2099 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2100 errorStr = "Cannot read file \'";
2101 errorStr += fileName;
2102 errorStr += "\'. Missing data.";
2103 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
2104 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2110 ep = strrchr(p, '\r');
2112 ep = strrchr(p, '\n');
2117 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2118 errorStr = "Cannot read file \'";
2119 errorStr += fileName;
2120 errorStr += "\'. Missing data.";
2121 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
2122 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
2127 if (str2x(p, *(d[pos])))
2129 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
2130 errorStr = "Cannot read file \'";
2131 errorStr += fileName;
2132 errorStr += "\'. Incorrect value. \'";
2135 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2142 memset(txt, 0, sizeof(txt));
2145 text->erase(text->begin(), text->end());
2146 while (!feof(msgFile))
2149 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2159 //-----------------------------------------------------------------------------
2160 int FILES_STORE::Touch(const std::string & path) const
2162 FILE * f = fopen(path.c_str(), "wb");
2170 //-----------------------------------------------------------------------------
2171 int GetFileList(vector<string> * fileList, const string & directory, mode_t mode, const string & ext)
2173 // æÕÎËÃÉÑ ÐÒÏÓÍÁÔÒÉ×ÁÅÔ ÓÏÄÅÒÖÉÍÏÅ ÄÉÒÅËÔÏÒÉÉ
2175 DIR * d = opendir(directory.c_str());
2179 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2184 while ((entry = readdir(d)))
2186 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2189 string str = directory + "/" + string(entry->d_name);
2192 if (stat(str.c_str(), &st))
2195 if (!(st.st_mode & mode)) // Filter by mode
2201 size_t d_nameLen = strlen(entry->d_name);
2202 if (d_nameLen <= ext.size())
2205 if (ext == entry->d_name + (d_nameLen - ext.size()))
2207 entry->d_name[d_nameLen - ext.size()] = 0;
2208 fileList->push_back(entry->d_name);
2213 fileList->push_back(entry->d_name);
2221 //-----------------------------------------------------------------------------