2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 * Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
23 $Date: 2010/10/07 19:53:11 $
46 #include "stg/common.h"
47 #include "stg/user_ips.h"
48 #include "stg/user_conf.h"
49 #include "stg/user_stat.h"
50 #include "stg/const.h"
51 #include "stg/blowfish.h"
52 #include "stg/logger.h"
53 #include "stg/locker.h"
54 #include "stg/plugin_creator.h"
55 #include "file_store.h"
57 #define DELETED_USERS_DIR "deleted_users"
59 #define adm_enc_passwd "cjeifY8m3"
61 int GetFileList(std::vector<std::string> * fileList, const std::string & directory, mode_t mode, const std::string & ext);
63 const int pt_mega = 1024 * 1024;
64 //-----------------------------------------------------------------------------
65 //-----------------------------------------------------------------------------
66 //-----------------------------------------------------------------------------
69 PLUGIN_CREATOR<FILES_STORE> fsc;
72 extern "C" STORE * GetStore();
73 //-----------------------------------------------------------------------------
74 //-----------------------------------------------------------------------------
75 //-----------------------------------------------------------------------------
78 return fsc.GetPlugin();
80 //-----------------------------------------------------------------------------
81 FILES_STORE_SETTINGS::FILES_STORE_SETTINGS()
101 //-----------------------------------------------------------------------------
102 int FILES_STORE_SETTINGS::ParseOwner(const std::vector<PARAM_VALUE> & moduleParams, const std::string & owner, uid_t * uid)
106 std::vector<PARAM_VALUE>::const_iterator pvi;
107 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
108 if (pvi == moduleParams.end())
110 errorStr = "Parameter \'" + owner + "\' not found.";
111 printfd(__FILE__, "%s\n", errorStr.c_str());
114 if (User2UID(pvi->value[0].c_str(), uid) < 0)
116 errorStr = "Parameter \'" + owner + "\': Unknown user \'" + pvi->value[0] + "\'";
117 printfd(__FILE__, "%s\n", errorStr.c_str());
122 //-----------------------------------------------------------------------------
123 int FILES_STORE_SETTINGS::ParseGroup(const std::vector<PARAM_VALUE> & moduleParams, const std::string & group, gid_t * gid)
127 std::vector<PARAM_VALUE>::const_iterator pvi;
128 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
129 if (pvi == moduleParams.end())
131 errorStr = "Parameter \'" + group + "\' not found.";
132 printfd(__FILE__, "%s\n", errorStr.c_str());
135 if (Group2GID(pvi->value[0].c_str(), gid) < 0)
137 errorStr = "Parameter \'" + group + "\': Unknown group \'" + pvi->value[0] + "\'";
138 printfd(__FILE__, "%s\n", errorStr.c_str());
143 //-----------------------------------------------------------------------------
144 int FILES_STORE_SETTINGS::ParseYesNo(const std::string & value, bool * val)
146 if (0 == strcasecmp(value.c_str(), "yes"))
151 if (0 == strcasecmp(value.c_str(), "no"))
157 errorStr = "Incorrect value \'" + value + "\'.";
160 //-----------------------------------------------------------------------------
161 int FILES_STORE_SETTINGS::ParseMode(const std::vector<PARAM_VALUE> & moduleParams, const std::string & modeStr, mode_t * mode)
165 std::vector<PARAM_VALUE>::const_iterator pvi;
166 pvi = find(moduleParams.begin(), moduleParams.end(), pv);
167 if (pvi == moduleParams.end())
169 errorStr = "Parameter \'" + modeStr + "\' not found.";
170 printfd(__FILE__, "%s\n", errorStr.c_str());
173 if (Str2Mode(pvi->value[0].c_str(), mode) < 0)
175 errorStr = "Parameter \'" + modeStr + "\': Incorrect mode \'" + pvi->value[0] + "\'";
176 printfd(__FILE__, "%s\n", errorStr.c_str());
181 //-----------------------------------------------------------------------------
182 int FILES_STORE_SETTINGS::ParseSettings(const MODULE_SETTINGS & s)
184 if (ParseOwner(s.moduleParams, "StatOwner", &statUID) < 0)
186 if (ParseGroup(s.moduleParams, "StatGroup", &statGID) < 0)
188 if (ParseMode(s.moduleParams, "StatMode", &statMode) < 0)
191 if (ParseOwner(s.moduleParams, "ConfOwner", &confUID) < 0)
193 if (ParseGroup(s.moduleParams, "ConfGroup", &confGID) < 0)
195 if (ParseMode(s.moduleParams, "ConfMode", &confMode) < 0)
198 if (ParseOwner(s.moduleParams, "UserLogOwner", &userLogUID) < 0)
200 if (ParseGroup(s.moduleParams, "UserLogGroup", &userLogGID) < 0)
202 if (ParseMode(s.moduleParams, "UserLogMode", &userLogMode) < 0)
205 std::vector<PARAM_VALUE>::const_iterator pvi;
207 pv.param = "RemoveBak";
208 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
209 if (pvi == s.moduleParams.end())
215 if (ParseYesNo(pvi->value[0], &removeBak))
217 printfd(__FILE__, "Cannot parse parameter 'RemoveBak'\n");
222 pv.param = "ReadBak";
223 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
224 if (pvi == s.moduleParams.end())
230 if (ParseYesNo(pvi->value[0], &readBak))
232 printfd(__FILE__, "Cannot parse parameter 'ReadBak'\n");
237 pv.param = "WorkDir";
238 pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv);
239 if (pvi == s.moduleParams.end())
241 errorStr = "Parameter \'WorkDir\' not found.";
242 printfd(__FILE__, "Parameter 'WorkDir' not found\n");
246 workDir = pvi->value[0];
247 if (workDir.size() && workDir[workDir.size() - 1] == '/')
249 workDir.resize(workDir.size() - 1);
251 usersDir = workDir + "/users/";
252 tariffsDir = workDir + "/tariffs/";
253 adminsDir = workDir + "/admins/";
257 //-----------------------------------------------------------------------------
258 const std::string & FILES_STORE_SETTINGS::GetStrError() const
262 //-----------------------------------------------------------------------------
263 int FILES_STORE_SETTINGS::User2UID(const char * user, uid_t * uid)
269 errorStr = std::string("User \'") + std::string(user) + std::string("\' not found in system.");
270 printfd(__FILE__, "%s\n", errorStr.c_str());
277 //-----------------------------------------------------------------------------
278 int FILES_STORE_SETTINGS::Group2GID(const char * gr, gid_t * gid)
284 errorStr = std::string("Group \'") + std::string(gr) + std::string("\' not found in system.");
285 printfd(__FILE__, "%s\n", errorStr.c_str());
292 //-----------------------------------------------------------------------------
293 int FILES_STORE_SETTINGS::Str2Mode(const char * str, mode_t * mode)
300 errorStr = std::string("Error parsing mode \'") + str + std::string("\'");
301 printfd(__FILE__, "%s\n", errorStr.c_str());
305 for (int i = 0; i < 3; i++)
306 if (str[i] > '7' || str[i] < '0')
308 errorStr = std::string("Error parsing mode \'") + str + std::string("\'");
309 printfd(__FILE__, "%s\n", errorStr.c_str());
317 *mode = ((mode_t)c) + ((mode_t)b << 3) + ((mode_t)a << 6);
321 //-----------------------------------------------------------------------------
322 mode_t FILES_STORE_SETTINGS::GetStatModeDir() const
324 mode_t mode = statMode;
325 if (statMode & S_IRUSR) mode |= S_IXUSR;
326 if (statMode & S_IRGRP) mode |= S_IXGRP;
327 if (statMode & S_IROTH) mode |= S_IXOTH;
330 //-----------------------------------------------------------------------------
331 mode_t FILES_STORE_SETTINGS::GetConfModeDir() const
333 mode_t mode = confMode;
334 if (confMode & S_IRUSR) mode |= S_IXUSR;
335 if (confMode & S_IRGRP) mode |= S_IXGRP;
336 if (confMode & S_IROTH) mode |= S_IXOTH;
339 //-----------------------------------------------------------------------------
340 //-----------------------------------------------------------------------------
341 //-----------------------------------------------------------------------------
342 FILES_STORE::FILES_STORE()
344 version("file_store v.1.04"),
348 logger(GetPluginLogger(GetStgLogger(), "store_files"))
350 pthread_mutexattr_t attr;
351 pthread_mutexattr_init(&attr);
352 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
353 pthread_mutex_init(&mutex, &attr);
355 //-----------------------------------------------------------------------------
356 int FILES_STORE::ParseSettings()
358 int ret = storeSettings.ParseSettings(settings);
361 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
362 errorStr = storeSettings.GetStrError();
366 //-----------------------------------------------------------------------------
367 int FILES_STORE::GetUsersList(std::vector<std::string> * userList) const
369 std::vector<std::string> files;
371 if (GetFileList(&files, storeSettings.GetUsersDir(), S_IFDIR, ""))
373 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
374 errorStr = "Failed to open '" + storeSettings.GetUsersDir() + "': " + std::string(strerror(errno));
378 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
380 userList->swap(files);
384 //-----------------------------------------------------------------------------
385 int FILES_STORE::GetAdminsList(std::vector<std::string> * adminList) const
387 std::vector<std::string> files;
389 if (GetFileList(&files, storeSettings.GetAdminsDir(), S_IFREG, ".adm"))
391 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
392 errorStr = "Failed to open '" + storeSettings.GetAdminsDir() + "': " + std::string(strerror(errno));
396 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
398 adminList->swap(files);
402 //-----------------------------------------------------------------------------
403 int FILES_STORE::GetTariffsList(std::vector<std::string> * tariffList) const
405 std::vector<std::string> files;
407 if (GetFileList(&files, storeSettings.GetTariffsDir(), S_IFREG, ".tf"))
409 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
410 errorStr = "Failed to open '" + storeSettings.GetTariffsDir() + "': " + std::string(strerror(errno));
414 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
416 tariffList->swap(files);
420 //-----------------------------------------------------------------------------
421 int FILES_STORE::RemoveDir(const char * path) const
423 DIR * d = opendir(path);
427 errorStr = "failed to open dir. Message: '";
428 errorStr += strerror(errno);
430 printfd(__FILE__, "FILE_STORE::RemoveDir() - Failed to open dir '%s': '%s'\n", path, strerror(errno));
435 while ((entry = readdir(d)))
437 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
440 std::string str = path;
441 str += "/" + std::string(entry->d_name);
444 if (stat(str.c_str(), &st))
447 if ((st.st_mode & S_IFREG))
449 if (unlink(str.c_str()))
451 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
452 errorStr = "unlink failed. Message: '";
453 errorStr += strerror(errno);
455 printfd(__FILE__, "FILES_STORE::RemoveDir() - unlink failed. Message: '%s'\n", strerror(errno));
461 if (!(st.st_mode & S_IFDIR))
463 if (RemoveDir(str.c_str()))
476 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
477 errorStr = "rmdir failed. Message: '";
478 errorStr += strerror(errno);
480 printfd(__FILE__, "FILES_STORE::RemoveDir() - rmdir failed. Message: '%s'\n", strerror(errno));
486 //-----------------------------------------------------------------------------
487 int FILES_STORE::AddUser(const std::string & login) const
489 std::string fileName;
491 strprintf(&fileName, "%s%s", storeSettings.GetUsersDir().c_str(), login.c_str());
493 if (mkdir(fileName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
495 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
496 errorStr = std::string("mkdir failed. Message: '") + strerror(errno) + "'";
497 printfd(__FILE__, "FILES_STORE::AddUser - mkdir failed. Message: '%s'\n", strerror(errno));
501 strprintf(&fileName, "%s%s/conf", storeSettings.GetUsersDir().c_str(), login.c_str());
504 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
505 errorStr = "Cannot create file \"" + fileName + "\'";
506 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
510 strprintf(&fileName, "%s%s/stat", storeSettings.GetUsersDir().c_str(), login.c_str());
513 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
514 errorStr = "Cannot create file \"" + fileName + "\'";
515 printfd(__FILE__, "FILES_STORE::AddUser - fopen failed. Message: '%s'\n", strerror(errno));
520 //-----------------------------------------------------------------------------
521 int FILES_STORE::DelUser(const std::string & login) const
524 std::string dirName1;
526 strprintf(&dirName, "%s/%s", storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR);
527 if (access(dirName.c_str(), F_OK) != 0)
529 if (mkdir(dirName.c_str(), 0700) != 0)
531 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
532 errorStr = "Directory '" + dirName + "' cannot be created.";
533 printfd(__FILE__, "FILES_STORE::DelUser - mkdir failed. Message: '%s'\n", strerror(errno));
538 if (access(dirName.c_str(), F_OK) == 0)
540 strprintf(&dirName, "%s/%s/%s.%lu", storeSettings.GetWorkDir().c_str(), DELETED_USERS_DIR, login.c_str(), time(NULL));
541 strprintf(&dirName1, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
542 if (rename(dirName1.c_str(), dirName.c_str()))
544 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
545 errorStr = "Error moving dir from " + dirName1 + " to " + dirName;
546 printfd(__FILE__, "FILES_STORE::DelUser - rename failed. Message: '%s'\n", strerror(errno));
552 strprintf(&dirName, "%s/%s", storeSettings.GetUsersDir().c_str(), login.c_str());
553 if (RemoveDir(dirName.c_str()))
560 //-----------------------------------------------------------------------------
561 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login) const
563 std::string fileName;
564 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
565 if (RestoreUserConf(conf, login, fileName))
567 if (!storeSettings.GetReadBak())
571 return RestoreUserConf(conf, login, fileName + ".bak");
575 //-----------------------------------------------------------------------------
576 int FILES_STORE::RestoreUserConf(USER_CONF * conf, const std::string & login, const std::string & fileName) const
578 CONFIGFILE cf(fileName);
584 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
585 errorStr = "User \'" + login + "\' data not read.";
586 printfd(__FILE__, "FILES_STORE::RestoreUserConf - conf read failed for user '%s'\n", login.c_str());
590 if (cf.ReadString("Password", &conf->password, "") < 0)
592 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
593 errorStr = "User \'" + login + "\' data not read. Parameter Password.";
594 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password read failed for user '%s'\n", login.c_str());
597 if (conf->password.empty())
599 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
600 errorStr = "User \'" + login + "\' password is blank.";
601 printfd(__FILE__, "FILES_STORE::RestoreUserConf - password is blank for user '%s'\n", login.c_str());
605 if (cf.ReadString("tariff", &conf->tariffName, "") < 0)
607 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
608 errorStr = "User \'" + login + "\' data not read. Parameter Tariff.";
609 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff read failed for user '%s'\n", login.c_str());
612 if (conf->tariffName.empty())
614 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
615 errorStr = "User \'" + login + "\' tariff is blank.";
616 printfd(__FILE__, "FILES_STORE::RestoreUserConf - tariff is blank for user '%s'\n", login.c_str());
621 cf.ReadString("IP", &ipStr, "?");
625 ips = StrToIPS(ipStr);
627 catch (const std::string & s)
629 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
630 errorStr = "User \'" + login + "\' data not read. Parameter IP address. " + s;
631 printfd(__FILE__, "FILES_STORE::RestoreUserConf - ip read failed for user '%s'\n", login.c_str());
636 if (cf.ReadInt("alwaysOnline", &conf->alwaysOnline, 0) != 0)
638 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
639 errorStr = "User \'" + login + "\' data not read. Parameter AlwaysOnline.";
640 printfd(__FILE__, "FILES_STORE::RestoreUserConf - alwaysonline read failed for user '%s'\n", login.c_str());
644 if (cf.ReadInt("down", &conf->disabled, 0) != 0)
646 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
647 errorStr = "User \'" + login + "\' data not read. Parameter Down.";
648 printfd(__FILE__, "FILES_STORE::RestoreUserConf - down read failed for user '%s'\n", login.c_str());
652 if (cf.ReadInt("passive", &conf->passive, 0) != 0)
654 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
655 errorStr = "User \'" + login + "\' data not read. Parameter Passive.";
656 printfd(__FILE__, "FILES_STORE::RestoreUserConf - passive read failed for user '%s'\n", login.c_str());
660 cf.ReadInt("DisabledDetailStat", &conf->disabledDetailStat, 0);
661 cf.ReadTime("CreditExpire", &conf->creditExpire, 0);
662 cf.ReadString("TariffChange", &conf->nextTariff, "");
663 cf.ReadString("Group", &conf->group, "");
664 cf.ReadString("RealName", &conf->realName, "");
665 cf.ReadString("Address", &conf->address, "");
666 cf.ReadString("Phone", &conf->phone, "");
667 cf.ReadString("Note", &conf->note, "");
668 cf.ReadString("email", &conf->email, "");
670 char userdataName[12];
671 for (int i = 0; i < USERDATA_NUM; i++)
673 snprintf(userdataName, 12, "Userdata%d", i);
674 cf.ReadString(userdataName, &conf->userdata[i], "");
677 if (cf.ReadDouble("Credit", &conf->credit, 0) != 0)
679 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
680 errorStr = "User \'" + login + "\' data not read. Parameter Credit.";
681 printfd(__FILE__, "FILES_STORE::RestoreUserConf - credit read failed for user '%s'\n", login.c_str());
687 //-----------------------------------------------------------------------------
688 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login) const
690 std::string fileName;
691 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
693 if (RestoreUserStat(stat, login, fileName))
695 if (!storeSettings.GetReadBak())
699 return RestoreUserStat(stat, login, fileName + ".bak");
703 //-----------------------------------------------------------------------------
704 int FILES_STORE::RestoreUserStat(USER_STAT * stat, const std::string & login, const std::string & fileName) const
706 CONFIGFILE cf(fileName);
712 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
713 errorStr = "User \'" + login + "\' stat not read. Cannot open file " + fileName + ".";
714 printfd(__FILE__, "FILES_STORE::RestoreUserStat - stat read failed for user '%s'\n", login.c_str());
720 for (int i = 0; i < DIR_NUM; i++)
723 snprintf(s, 22, "D%d", i);
724 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
726 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
727 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
728 printfd(__FILE__, "FILES_STORE::RestoreUserStat - download stat read failed for user '%s'\n", login.c_str());
731 stat->down[i] = traff;
733 snprintf(s, 22, "U%d", i);
734 if (cf.ReadULongLongInt(s, &traff, 0) != 0)
736 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
737 errorStr = "User \'" + login + "\' stat not read. Parameter " + std::string(s);
738 printfd(__FILE__, "FILES_STORE::RestoreUserStat - upload stat read failed for user '%s'\n", login.c_str());
744 if (cf.ReadDouble("Cash", &stat->cash, 0) != 0)
746 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
747 errorStr = "User \'" + login + "\' stat not read. Parameter Cash";
748 printfd(__FILE__, "FILES_STORE::RestoreUserStat - cash read failed for user '%s'\n", login.c_str());
752 if (cf.ReadDouble("FreeMb", &stat->freeMb, 0) != 0)
754 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
755 errorStr = "User \'" + login + "\' stat not read. Parameter FreeMb";
756 printfd(__FILE__, "FILES_STORE::RestoreUserStat - freemb read failed for user '%s'\n", login.c_str());
760 if (cf.ReadTime("LastCashAddTime", &stat->lastCashAddTime, 0) != 0)
762 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
763 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAddTime";
764 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashaddtime read failed for user '%s'\n", login.c_str());
768 if (cf.ReadTime("PassiveTime", &stat->passiveTime, 0) != 0)
770 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
771 errorStr = "User \'" + login + "\' stat not read. Parameter PassiveTime";
772 printfd(__FILE__, "FILES_STORE::RestoreUserStat - passivetime read failed for user '%s'\n", login.c_str());
776 if (cf.ReadDouble("LastCashAdd", &stat->lastCashAdd, 0) != 0)
778 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
779 errorStr = "User \'" + login + "\' stat not read. Parameter LastCashAdd";
780 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastcashadd read failed for user '%s'\n", login.c_str());
784 if (cf.ReadTime("LastActivityTime", &stat->lastActivityTime, 0) != 0)
786 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
787 errorStr = "User \'" + login + "\' stat not read. Parameter LastActivityTime";
788 printfd(__FILE__, "FILES_STORE::RestoreUserStat - lastactivitytime read failed for user '%s'\n", login.c_str());
794 //-----------------------------------------------------------------------------
795 int FILES_STORE::SaveUserConf(const USER_CONF & conf, const std::string & login) const
797 std::string fileName;
798 fileName = storeSettings.GetUsersDir() + "/" + login + "/conf";
800 CONFIGFILE cfstat(fileName, true);
802 int e = cfstat.Error();
806 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
807 errorStr = std::string("User \'") + login + "\' conf not written\n";
808 printfd(__FILE__, "FILES_STORE::SaveUserConf - conf write failed for user '%s'\n", login.c_str());
812 e = chmod(fileName.c_str(), storeSettings.GetConfMode());
813 e += chown(fileName.c_str(), storeSettings.GetConfUID(), storeSettings.GetConfGID());
817 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
818 printfd(__FILE__, "FILES_STORE::SaveUserConf - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
821 cfstat.WriteString("Password", conf.password);
822 cfstat.WriteInt ("Passive", conf.passive);
823 cfstat.WriteInt ("Down", conf.disabled);
824 cfstat.WriteInt("DisabledDetailStat", conf.disabledDetailStat);
825 cfstat.WriteInt ("AlwaysOnline", conf.alwaysOnline);
826 cfstat.WriteString("Tariff", conf.tariffName);
827 cfstat.WriteString("Address", conf.address);
828 cfstat.WriteString("Phone", conf.phone);
829 cfstat.WriteString("Email", conf.email);
830 cfstat.WriteString("Note", conf.note);
831 cfstat.WriteString("RealName", conf.realName);
832 cfstat.WriteString("Group", conf.group);
833 cfstat.WriteDouble("Credit", conf.credit);
834 cfstat.WriteString("TariffChange", conf.nextTariff);
836 char userdataName[12];
837 for (int i = 0; i < USERDATA_NUM; i++)
839 snprintf(userdataName, 12, "Userdata%d", i);
840 cfstat.WriteString(userdataName, conf.userdata[i]);
842 cfstat.WriteInt("CreditExpire", conf.creditExpire);
844 std::ostringstream ipStr;
846 cfstat.WriteString("IP", ipStr.str());
850 //-----------------------------------------------------------------------------
851 int FILES_STORE::SaveUserStat(const USER_STAT & stat, const std::string & login) const
854 std::string fileName;
855 fileName = storeSettings.GetUsersDir() + "/" + login + "/stat";
858 CONFIGFILE cfstat(fileName, true);
859 int e = cfstat.Error();
863 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
864 errorStr = std::string("User \'") + login + "\' stat not written\n";
865 printfd(__FILE__, "FILES_STORE::SaveUserStat - stat write failed for user '%s'\n", login.c_str());
869 for (int i = 0; i < DIR_NUM; i++)
871 snprintf(s, 22, "D%d", i);
872 cfstat.WriteInt(s, stat.down[i]);
873 snprintf(s, 22, "U%d", i);
874 cfstat.WriteInt(s, stat.up[i]);
877 cfstat.WriteDouble("Cash", stat.cash);
878 cfstat.WriteDouble("FreeMb", stat.freeMb);
879 cfstat.WriteDouble("LastCashAdd", stat.lastCashAdd);
880 cfstat.WriteInt("LastCashAddTime", stat.lastCashAddTime);
881 cfstat.WriteInt("PassiveTime", stat.passiveTime);
882 cfstat.WriteInt("LastActivityTime", stat.lastActivityTime);
885 int e = chmod(fileName.c_str(), storeSettings.GetStatMode());
886 e += chown(fileName.c_str(), storeSettings.GetStatUID(), storeSettings.GetStatGID());
890 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
891 printfd(__FILE__, "FILES_STORE::SaveUserStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
896 //-----------------------------------------------------------------------------
897 int FILES_STORE::WriteLogString(const std::string & str, const std::string & login) const
900 time_t tm = time(NULL);
901 std::string fileName;
902 fileName = storeSettings.GetUsersDir() + "/" + login + "/log";
903 f = fopen(fileName.c_str(), "at");
907 fprintf(f, "%s", LogDate(tm));
909 fprintf(f, "%s", str.c_str());
915 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
916 errorStr = "Cannot open \'" + fileName + "\'";
917 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
921 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
922 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
926 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
927 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
932 //-----------------------------------------------------------------------------
933 int FILES_STORE::WriteLog2String(const std::string & str, const std::string & login) const
936 time_t tm = time(NULL);
937 std::string fileName;
938 fileName = storeSettings.GetUsersDir() + "/" + login + "/log2";
939 f = fopen(fileName.c_str(), "at");
943 fprintf(f, "%s", LogDate(tm));
945 fprintf(f, "%s", str.c_str());
951 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
952 errorStr = "Cannot open \'" + fileName + "\'";
953 printfd(__FILE__, "FILES_STORE::WriteLogString - log write failed for user '%s'\n", login.c_str());
957 int e = chmod(fileName.c_str(), storeSettings.GetLogMode());
958 e += chown(fileName.c_str(), storeSettings.GetLogUID(), storeSettings.GetLogGID());
962 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
963 printfd(__FILE__, "FILES_STORE::WriteLogString - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
968 //-----------------------------------------------------------------------------
969 int FILES_STORE::WriteUserChgLog(const std::string & login,
970 const std::string & admLogin,
972 const std::string & paramName,
973 const std::string & oldValue,
974 const std::string & newValue,
975 const std::string & message) const
977 std::string userLogMsg = "Admin \'" + admLogin + "\', " + inet_ntostring(admIP) + ": \'"
978 + paramName + "\' parameter changed from \'" + oldValue +
979 "\' to \'" + newValue + "\'. " + message;
981 return WriteLogString(userLogMsg, login);
983 //-----------------------------------------------------------------------------
984 int FILES_STORE::WriteUserConnect(const std::string & login, uint32_t ip) const
986 std::string logStr = "Connect, " + inet_ntostring(ip);
987 if (WriteLogString(logStr, login))
989 return WriteLog2String(logStr, login);
991 //-----------------------------------------------------------------------------
992 int FILES_STORE::WriteUserDisconnect(const std::string & login,
993 const DIR_TRAFF & up,
994 const DIR_TRAFF & down,
995 const DIR_TRAFF & sessionUp,
996 const DIR_TRAFF & sessionDown,
999 const std::string & reason) const
1001 std::ostringstream logStr;
1002 logStr << "Disconnect, "
1003 << " session upload: \'"
1005 << "\' session download: \'"
1007 << "\' month upload: \'"
1009 << "\' month download: \'"
1015 if (WriteLogString(logStr.str(), login))
1018 logStr << " freeMb: \'"
1025 return WriteLog2String(logStr.str(), login);
1027 //-----------------------------------------------------------------------------
1028 int FILES_STORE::SaveMonthStat(const USER_STAT & stat, int month, int year, const std::string & login) const
1032 strprintf(&stat1,"%s/%s/stat.%d.%02d",
1033 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1035 CONFIGFILE s(stat1, true);
1039 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1040 errorStr = "Cannot create file '" + stat1 + "'";
1041 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1047 strprintf(&stat2,"%s/%s/stat2.%d.%02d",
1048 storeSettings.GetUsersDir().c_str(), login.c_str(), year + 1900, month + 1);
1050 CONFIGFILE s2(stat2, true);
1054 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1055 errorStr = "Cannot create file '" + stat2 + "'";
1056 printfd(__FILE__, "FILES_STORE::SaveMonthStat - month stat write failed for user '%s'\n", login.c_str());
1060 for (size_t i = 0; i < DIR_NUM; i++)
1063 snprintf(dirName, 3, "U%llu", (unsigned long long)i);
1064 s.WriteInt(dirName, stat.up[i]); // Classic
1065 s2.WriteInt(dirName, stat.up[i]); // New
1066 snprintf(dirName, 3, "D%llu", (unsigned long long)i);
1067 s.WriteInt(dirName, stat.down[i]); // Classic
1068 s2.WriteInt(dirName, stat.down[i]); // New
1072 s.WriteDouble("cash", stat.cash);
1075 s2.WriteDouble("Cash", stat.cash);
1076 s2.WriteDouble("FreeMb", stat.freeMb);
1077 s2.WriteDouble("LastCashAdd", stat.lastCashAdd);
1078 s2.WriteInt("LastCashAddTime", stat.lastCashAddTime);
1079 s2.WriteInt("PassiveTime", stat.passiveTime);
1080 s2.WriteInt("LastActivityTime", stat.lastActivityTime);
1084 //-----------------------------------------------------------------------------*/
1085 int FILES_STORE::AddAdmin(const std::string & login) const
1087 std::string fileName;
1088 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1090 if (Touch(fileName))
1092 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1093 errorStr = "Cannot create file " + fileName;
1094 printfd(__FILE__, "FILES_STORE::AddAdmin - failed to add admin '%s'\n", login.c_str());
1100 //-----------------------------------------------------------------------------*/
1101 int FILES_STORE::DelAdmin(const std::string & login) const
1103 std::string fileName;
1104 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1105 if (unlink(fileName.c_str()))
1107 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1108 errorStr = "unlink failed. Message: '";
1109 errorStr += strerror(errno);
1111 printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno));
1115 //-----------------------------------------------------------------------------*/
1116 int FILES_STORE::SaveAdmin(const ADMIN_CONF & ac) const
1118 char passwordE[2 * ADM_PASSWD_LEN + 2];
1119 char pass[ADM_PASSWD_LEN + 1];
1120 char adminPass[ADM_PASSWD_LEN + 1];
1122 std::string fileName;
1124 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), ac.login.c_str());
1127 CONFIGFILE cf(fileName, true);
1133 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1134 errorStr = "Cannot write admin " + ac.login + ". " + fileName;
1135 printfd(__FILE__, "FILES_STORE::SaveAdmin - failed to save admin '%s'\n", ac.login.c_str());
1139 memset(pass, 0, sizeof(pass));
1140 memset(adminPass, 0, sizeof(adminPass));
1143 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1145 strncpy(adminPass, ac.password.c_str(), ADM_PASSWD_LEN);
1146 adminPass[ADM_PASSWD_LEN - 1] = 0;
1148 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1150 EncodeString(pass + 8*i, adminPass + 8*i, &ctx);
1153 pass[ADM_PASSWD_LEN - 1] = 0;
1154 Encode12(passwordE, pass, ADM_PASSWD_LEN);
1156 cf.WriteString("password", passwordE);
1157 cf.WriteInt("ChgConf", ac.priv.userConf);
1158 cf.WriteInt("ChgPassword", ac.priv.userPasswd);
1159 cf.WriteInt("ChgStat", ac.priv.userStat);
1160 cf.WriteInt("ChgCash", ac.priv.userCash);
1161 cf.WriteInt("UsrAddDel", ac.priv.userAddDel);
1162 cf.WriteInt("ChgTariff", ac.priv.tariffChg);
1163 cf.WriteInt("ChgAdmin", ac.priv.adminChg);
1164 cf.WriteInt("ChgService", ac.priv.serviceChg);
1165 cf.WriteInt("ChgCorp", ac.priv.corpChg);
1170 //-----------------------------------------------------------------------------
1171 int FILES_STORE::RestoreAdmin(ADMIN_CONF * ac, const std::string & login) const
1173 std::string fileName;
1174 strprintf(&fileName, "%s/%s.adm", storeSettings.GetAdminsDir().c_str(), login.c_str());
1175 CONFIGFILE cf(fileName);
1176 char pass[ADM_PASSWD_LEN + 1];
1177 char password[ADM_PASSWD_LEN + 1];
1178 char passwordE[2 * ADM_PASSWD_LEN + 2];
1185 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1186 errorStr = "Cannot open " + fileName;
1187 printfd(__FILE__, "FILES_STORE::RestoreAdmin - failed to restore admin '%s'\n", ac->login.c_str());
1191 if (cf.ReadString("password", &p, "*"))
1193 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1194 errorStr = "Error in parameter password";
1195 printfd(__FILE__, "FILES_STORE::RestoreAdmin - password read failed for admin '%s'\n", ac->login.c_str());
1199 memset(passwordE, 0, sizeof(passwordE));
1200 strncpy(passwordE, p.c_str(), 2*ADM_PASSWD_LEN);
1202 memset(pass, 0, sizeof(pass));
1204 if (passwordE[0] != 0)
1206 Decode21(pass, passwordE);
1207 EnDecodeInit(adm_enc_passwd, strlen(adm_enc_passwd), &ctx);
1209 for (int i = 0; i < ADM_PASSWD_LEN/8; i++)
1211 DecodeString(password + 8*i, pass + 8*i, &ctx);
1219 ac->password = password;
1223 if (cf.ReadUShortInt("ChgConf", &a, 0) == 0)
1224 ac->priv.userConf = a;
1227 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1228 errorStr = "Error in parameter ChgConf";
1229 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgconf read failed for admin '%s'\n", ac->login.c_str());
1233 if (cf.ReadUShortInt("ChgPassword", &a, 0) == 0)
1234 ac->priv.userPasswd = a;
1237 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1238 errorStr = "Error in parameter ChgPassword";
1239 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgpassword read failed for admin '%s'\n", ac->login.c_str());
1243 if (cf.ReadUShortInt("ChgStat", &a, 0) == 0)
1244 ac->priv.userStat = a;
1247 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1248 errorStr = "Error in parameter ChgStat";
1249 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgstat read failed for admin '%s'\n", ac->login.c_str());
1253 if (cf.ReadUShortInt("ChgCash", &a, 0) == 0)
1254 ac->priv.userCash = a;
1257 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1258 errorStr = "Error in parameter ChgCash";
1259 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgcash read failed for admin '%s'\n", ac->login.c_str());
1263 if (cf.ReadUShortInt("UsrAddDel", &a, 0) == 0)
1264 ac->priv.userAddDel = a;
1267 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1268 errorStr = "Error in parameter UsrAddDel";
1269 printfd(__FILE__, "FILES_STORE::RestoreAdmin - usradddel read failed for admin '%s'\n", ac->login.c_str());
1273 if (cf.ReadUShortInt("ChgAdmin", &a, 0) == 0)
1274 ac->priv.adminChg = a;
1277 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1278 errorStr = "Error in parameter ChgAdmin";
1279 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgadmin read failed for admin '%s'\n", ac->login.c_str());
1283 if (cf.ReadUShortInt("ChgTariff", &a, 0) == 0)
1284 ac->priv.tariffChg = a;
1287 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1288 errorStr = "Error in parameter ChgTariff";
1289 printfd(__FILE__, "FILES_STORE::RestoreAdmin - chgtariff read failed for admin '%s'\n", ac->login.c_str());
1293 if (cf.ReadUShortInt("ChgService", &a, 0) == 0)
1294 ac->priv.serviceChg = a;
1296 ac->priv.serviceChg = 0;
1298 if (cf.ReadUShortInt("ChgCorp", &a, 0) == 0)
1299 ac->priv.corpChg = a;
1301 ac->priv.corpChg = 0;
1305 //-----------------------------------------------------------------------------
1306 int FILES_STORE::AddTariff(const std::string & name) const
1308 std::string fileName;
1309 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1310 if (Touch(fileName))
1312 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1313 errorStr = "Cannot create file " + fileName;
1314 printfd(__FILE__, "FILES_STORE::AddTariff - failed to add tariff '%s'\n", name.c_str());
1319 //-----------------------------------------------------------------------------
1320 int FILES_STORE::DelTariff(const std::string & name) const
1322 std::string fileName;
1323 strprintf(&fileName, "%s/%s.tf", storeSettings.GetTariffsDir().c_str(), name.c_str());
1324 if (unlink(fileName.c_str()))
1326 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1327 errorStr = "unlink failed. Message: '";
1328 errorStr += strerror(errno);
1330 printfd(__FILE__, "FILES_STORE::DelTariff - unlink failed. Message: '%s'\n", strerror(errno));
1334 //-----------------------------------------------------------------------------
1335 int FILES_STORE::RestoreTariff(TARIFF_DATA * td, const std::string & tariffName) const
1337 std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1338 CONFIGFILE conf(fileName);
1340 td->tariffConf.name = tariffName;
1342 if (conf.Error() != 0)
1344 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1345 errorStr = "Cannot read file " + fileName;
1346 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to read tariff '%s'\n", tariffName.c_str());
1351 for (int i = 0; i<DIR_NUM; i++)
1353 strprintf(¶m, "Time%d", i);
1354 if (conf.ReadString(param, &str, "00:00-00:00") < 0)
1356 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1357 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1358 printfd(__FILE__, "FILES_STORE::RestoreTariff - time%d read failed for tariff '%s'\n", i, tariffName.c_str());
1362 ParseTariffTimeStr(str.c_str(),
1363 td->dirPrice[i].hDay,
1364 td->dirPrice[i].mDay,
1365 td->dirPrice[i].hNight,
1366 td->dirPrice[i].mNight);
1368 strprintf(¶m, "PriceDayA%d", i);
1369 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayA, 0.0) < 0)
1371 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1372 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1373 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedaya read failed for tariff '%s'\n", tariffName.c_str());
1376 td->dirPrice[i].priceDayA /= (1024*1024);
1378 strprintf(¶m, "PriceDayB%d", i);
1379 if (conf.ReadDouble(param, &td->dirPrice[i].priceDayB, 0.0) < 0)
1381 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1382 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1383 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricedayb read failed for tariff '%s'\n", tariffName.c_str());
1386 td->dirPrice[i].priceDayB /= (1024*1024);
1388 strprintf(¶m, "PriceNightA%d", i);
1389 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightA, 0.0) < 0)
1391 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1392 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1393 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenighta read failed for tariff '%s'\n", tariffName.c_str());
1396 td->dirPrice[i].priceNightA /= (1024*1024);
1398 strprintf(¶m, "PriceNightB%d", i);
1399 if (conf.ReadDouble(param, &td->dirPrice[i].priceNightB, 0.0) < 0)
1401 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1402 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1403 printfd(__FILE__, "FILES_STORE::RestoreTariff - pricenightb read failed for tariff '%s'\n", tariffName.c_str());
1406 td->dirPrice[i].priceNightB /= (1024*1024);
1408 strprintf(¶m, "Threshold%d", i);
1409 if (conf.ReadInt(param, &td->dirPrice[i].threshold, 0) < 0)
1411 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1412 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1413 printfd(__FILE__, "FILES_STORE::RestoreTariff - threshold read failed for tariff '%s'\n", tariffName.c_str());
1417 strprintf(¶m, "SinglePrice%d", i);
1418 if (conf.ReadInt(param, &td->dirPrice[i].singlePrice, 0) < 0)
1420 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1421 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1422 printfd(__FILE__, "FILES_STORE::RestoreTariff - singleprice read failed for tariff '%s'\n", tariffName.c_str());
1426 strprintf(¶m, "NoDiscount%d", i);
1427 if (conf.ReadInt(param, &td->dirPrice[i].noDiscount, 0) < 0)
1429 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1430 errorStr = "Cannot read tariff " + tariffName + ". Parameter " + param;
1431 printfd(__FILE__, "FILES_STORE::RestoreTariff - nodiscount read failed for tariff '%s'\n", tariffName.c_str());
1436 if (conf.ReadDouble("Fee", &td->tariffConf.fee, 0) < 0)
1438 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1439 errorStr = "Cannot read tariff " + tariffName + ". Parameter Fee";
1440 printfd(__FILE__, "FILES_STORE::RestoreTariff - fee read failed for tariff '%s'\n", tariffName.c_str());
1444 if (conf.ReadDouble("Free", &td->tariffConf.free, 0) < 0)
1446 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1447 errorStr = "Cannot read tariff " + tariffName + ". Parameter Free";
1448 printfd(__FILE__, "FILES_STORE::RestoreTariff - free read failed for tariff '%s'\n", tariffName.c_str());
1452 if (conf.ReadDouble("PassiveCost", &td->tariffConf.passiveCost, 0) < 0)
1454 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1455 errorStr = "Cannot read tariff " + tariffName + ". Parameter PassiveCost";
1456 printfd(__FILE__, "FILES_STORE::RestoreTariff - passivecost read failed for tariff '%s'\n", tariffName.c_str());
1460 if (conf.ReadString("TraffType", &str, "") < 0)
1462 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1463 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType";
1464 printfd(__FILE__, "FILES_STORE::RestoreTariff - trafftype read failed for tariff '%s'\n", tariffName.c_str());
1468 if (!strcasecmp(str.c_str(), "up"))
1469 td->tariffConf.traffType = TRAFF_UP;
1471 if (!strcasecmp(str.c_str(), "down"))
1472 td->tariffConf.traffType = TRAFF_DOWN;
1474 if (!strcasecmp(str.c_str(), "up+down"))
1475 td->tariffConf.traffType = TRAFF_UP_DOWN;
1477 if (!strcasecmp(str.c_str(), "max"))
1478 td->tariffConf.traffType = TRAFF_MAX;
1481 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1482 errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect";
1483 printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str());
1488 //-----------------------------------------------------------------------------
1489 int FILES_STORE::SaveTariff(const TARIFF_DATA & td, const std::string & tariffName) const
1491 std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf";
1494 CONFIGFILE cf(fileName, true);
1500 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1501 errorStr = "Error writing tariff " + tariffName;
1502 printfd(__FILE__, "FILES_STORE::RestoreTariff - failed to save tariff '%s'\n", tariffName.c_str());
1507 for (int i = 0; i < DIR_NUM; i++)
1509 strprintf(¶m, "PriceDayA%d", i);
1510 cf.WriteDouble(param, td.dirPrice[i].priceDayA * pt_mega);
1512 strprintf(¶m, "PriceDayB%d", i);
1513 cf.WriteDouble(param, td.dirPrice[i].priceDayB * pt_mega);
1515 strprintf(¶m, "PriceNightA%d", i);
1516 cf.WriteDouble(param, td.dirPrice[i].priceNightA * pt_mega);
1518 strprintf(¶m, "PriceNightB%d", i);
1519 cf.WriteDouble(param, td.dirPrice[i].priceNightB * pt_mega);
1521 strprintf(¶m, "Threshold%d", i);
1522 cf.WriteInt(param, td.dirPrice[i].threshold);
1525 strprintf(¶m, "Time%d", i);
1527 strprintf(&s, "%0d:%0d-%0d:%0d",
1528 td.dirPrice[i].hDay,
1529 td.dirPrice[i].mDay,
1530 td.dirPrice[i].hNight,
1531 td.dirPrice[i].mNight);
1533 cf.WriteString(param, s);
1535 strprintf(¶m, "NoDiscount%d", i);
1536 cf.WriteInt(param, td.dirPrice[i].noDiscount);
1538 strprintf(¶m, "SinglePrice%d", i);
1539 cf.WriteInt(param, td.dirPrice[i].singlePrice);
1542 cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost);
1543 cf.WriteDouble("Fee", td.tariffConf.fee);
1544 cf.WriteDouble("Free", td.tariffConf.free);
1546 switch (td.tariffConf.traffType)
1549 cf.WriteString("TraffType", "up");
1552 cf.WriteString("TraffType", "down");
1555 cf.WriteString("TraffType", "up+down");
1558 cf.WriteString("TraffType", "max");
1565 //-----------------------------------------------------------------------------
1566 int FILES_STORE::WriteDetailedStat(const std::map<IP_DIR_PAIR, STAT_NODE> & statTree,
1568 const std::string & login) const
1570 char fn[FN_STR_LEN];
1571 char dn[FN_STR_LEN];
1578 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat", storeSettings.GetUsersDir().c_str(), login.c_str());
1579 if (access(dn, F_OK) != 0)
1581 if (mkdir(dn, 0700) != 0)
1583 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1584 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1585 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1590 int e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1591 e += chmod(dn, storeSettings.GetStatModeDir());
1595 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1596 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1601 if (lt->tm_hour == 0 && lt->tm_min <= 5)
1607 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d",
1608 storeSettings.GetUsersDir().c_str(),
1612 if (access(dn, F_OK) != 0)
1614 if (mkdir(dn, 0700) != 0)
1616 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1617 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1618 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1623 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1624 e += chmod(dn, storeSettings.GetStatModeDir());
1628 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1629 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1632 snprintf(dn, FN_STR_LEN, "%s/%s/detail_stat/%d/%s%d",
1633 storeSettings.GetUsersDir().c_str(),
1636 lt->tm_mon+1 < 10 ? "0" : "",
1638 if (access(dn, F_OK) != 0)
1640 if (mkdir(dn, 0700) != 0)
1642 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1643 errorStr = "Directory \'" + std::string(dn) + "\' cannot be created.";
1644 printfd(__FILE__, "FILES_STORE::WriteDetailStat - mkdir failed. Message: '%s'\n", strerror(errno));
1649 e = chown(dn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1650 e += chmod(dn, storeSettings.GetStatModeDir());
1654 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1655 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1658 snprintf(fn, FN_STR_LEN, "%s/%s%d", dn, lt->tm_mday < 10 ? "0" : "", lt->tm_mday);
1660 statFile = fopen (fn, "at");
1664 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1665 errorStr = "File \'" + std::string(fn) + "\' cannot be written.";
1666 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fopen failed. Message: '%s'\n", strerror(errno));
1673 lt1 = localtime(&lastStat);
1682 lt2 = localtime(&t);
1688 if (fprintf(statFile, "-> %02d.%02d.%02d - %02d.%02d.%02d\n",
1689 h1, m1, s1, h2, m2, s2) < 0)
1691 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1692 errorStr = std::string("fprint failed. Message: '") + strerror(errno) + "'";
1693 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1698 std::map<IP_DIR_PAIR, STAT_NODE>::const_iterator stIter;
1699 stIter = statTree.begin();
1701 while (stIter != statTree.end())
1704 x2str(stIter->second.up, u);
1705 x2str(stIter->second.down, d);
1706 #ifdef TRAFF_STAT_WITH_PORTS
1707 if (fprintf(statFile, "%17s:%hu\t%15d\t%15s\t%15s\t%f\n",
1708 inet_ntostring(stIter->first.ip).c_str(),
1713 stIter->second.cash) < 0)
1715 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1716 errorStr = "fprint failed. Message: '";
1717 errorStr += strerror(errno);
1719 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1724 if (fprintf(statFile, "%17s\t%15d\t%15s\t%15s\t%f\n",
1725 inet_ntostring(stIter->first.ip).c_str(),
1729 stIter->second.cash) < 0)
1731 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1732 errorStr = std::string("fprint failed. Message: '");
1733 errorStr += strerror(errno);
1735 printfd(__FILE__, "FILES_STORE::WriteDetailStat - fprintf failed. Message: '%s'\n", strerror(errno));
1746 e = chown(fn, storeSettings.GetStatUID(), storeSettings.GetStatGID());
1747 e += chmod(fn, storeSettings.GetStatMode());
1751 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1752 printfd(__FILE__, "FILES_STORE::WriteDetailStat - chmod/chown failed for user '%s'. Error: '%s'\n", login.c_str(), strerror(errno));
1757 //-----------------------------------------------------------------------------
1758 int FILES_STORE::AddMessage(STG_MSG * msg, const std::string & login) const
1764 strprintf(&dn, "%s/%s/messages", storeSettings.GetUsersDir().c_str(), login.c_str());
1765 if (access(dn.c_str(), F_OK) != 0)
1767 if (mkdir(dn.c_str(), 0700) != 0)
1769 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1770 errorStr = "Directory \'";
1772 errorStr += "\' cannot be created.";
1773 printfd(__FILE__, "FILES_STORE::AddMessage - mkdir failed. Message: '%s'\n", strerror(errno));
1778 chmod(dn.c_str(), storeSettings.GetConfModeDir());
1780 gettimeofday(&tv, NULL);
1782 msg->header.id = ((long long)tv.tv_sec) * 1000000 + ((long long)tv.tv_usec);
1783 strprintf(&fn, "%s/%lld", dn.c_str(), msg->header.id);
1787 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1788 errorStr = "File \'";
1790 errorStr += "\' cannot be writen.";
1791 printfd(__FILE__, "FILES_STORE::AddMessage - fopen failed. Message: '%s'\n", strerror(errno));
1795 return EditMessage(*msg, login);
1797 //-----------------------------------------------------------------------------
1798 int FILES_STORE::EditMessage(const STG_MSG & msg, const std::string & login) const
1800 std::string fileName;
1803 strprintf(&fileName, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), msg.header.id);
1805 if (access(fileName.c_str(), F_OK) != 0)
1808 x2str(msg.header.id, idstr);
1809 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1810 errorStr = "Message for user \'";
1811 errorStr += login + "\' with ID \'";
1812 errorStr += idstr + "\' does not exist.";
1813 printfd(__FILE__, "FILES_STORE::EditMessage - %s\n", errorStr.c_str());
1817 Touch(fileName + ".new");
1819 msgFile = fopen((fileName + ".new").c_str(), "wt");
1822 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1823 errorStr = "File \'" + fileName + "\' cannot be writen.";
1824 printfd(__FILE__, "FILES_STORE::EditMessage - fopen failed. Message: '%s'\n", strerror(errno));
1829 res &= (fprintf(msgFile, "%d\n", msg.header.type) >= 0);
1830 res &= (fprintf(msgFile, "%u\n", msg.header.lastSendTime) >= 0);
1831 res &= (fprintf(msgFile, "%u\n", msg.header.creationTime) >= 0);
1832 res &= (fprintf(msgFile, "%u\n", msg.header.showTime) >= 0);
1833 res &= (fprintf(msgFile, "%d\n", msg.header.repeat) >= 0);
1834 res &= (fprintf(msgFile, "%u\n", msg.header.repeatPeriod) >= 0);
1835 res &= (fprintf(msgFile, "%s", msg.text.c_str()) >= 0);
1839 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1840 errorStr = std::string("fprintf failed. Message: '") + strerror(errno) + "'";
1841 printfd(__FILE__, "FILES_STORE::EditMessage - fprintf failed. Message: '%s'\n", strerror(errno));
1848 chmod((fileName + ".new").c_str(), storeSettings.GetConfMode());
1850 if (rename((fileName + ".new").c_str(), fileName.c_str()) < 0)
1852 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1853 errorStr = "Error moving dir from " + fileName + ".new to " + fileName;
1854 printfd(__FILE__, "FILES_STORE::SaveTariff - rename failed. Message: '%s'\n", strerror(errno));
1860 //-----------------------------------------------------------------------------
1861 int FILES_STORE::GetMessage(uint64_t id, STG_MSG * msg, const std::string & login) const
1864 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1865 msg->header.id = id;
1866 return ReadMessage(fn, &msg->header, &msg->text);
1868 //-----------------------------------------------------------------------------
1869 int FILES_STORE::DelMessage(uint64_t id, const std::string & login) const
1872 strprintf(&fn, "%s/%s/messages/%lld", storeSettings.GetUsersDir().c_str(), login.c_str(), id);
1874 return unlink(fn.c_str());
1876 //-----------------------------------------------------------------------------
1877 int FILES_STORE::GetMessageHdrs(std::vector<STG_MSG_HDR> * hdrsList, const std::string & login) const
1879 std::string dn(storeSettings.GetUsersDir() + "/" + login + "/messages/");
1881 if (access(dn.c_str(), F_OK) != 0)
1886 std::vector<std::string> messages;
1887 GetFileList(&messages, dn, S_IFREG, "");
1889 for (unsigned i = 0; i < messages.size(); i++)
1891 unsigned long long id = 0;
1893 if (str2x(messages[i].c_str(), id))
1895 if (unlink((dn + messages[i]).c_str()))
1897 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1898 errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
1899 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
1906 if (ReadMessage(dn + messages[i], &hdr, NULL))
1913 if (unlink((dn + messages[i]).c_str()))
1915 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1916 errorStr = std::string("unlink failed. Message: '") + strerror(errno) + "'";
1917 printfd(__FILE__, "FILES_STORE::GetMessageHdrs - unlink failed. Message: '%s'\n", strerror(errno));
1924 hdrsList->push_back(hdr);
1928 //-----------------------------------------------------------------------------
1929 int FILES_STORE::ReadMessage(const std::string & fileName,
1931 std::string * text) const
1934 msgFile = fopen(fileName.c_str(), "rt");
1937 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1938 errorStr = "File \'";
1939 errorStr += fileName;
1940 errorStr += "\' cannot be openned.";
1941 printfd(__FILE__, "FILES_STORE::ReadMessage - fopen failed. Message: '%s'\n", strerror(errno));
1947 d[1] = &hdr->lastSendTime;
1948 d[2] = &hdr->creationTime;
1949 d[3] = &hdr->showTime;
1950 d[4] = (unsigned*)(&hdr->repeat);
1951 d[5] = &hdr->repeatPeriod;
1953 memset(p, 0, sizeof(p));
1955 for (int pos = 0; pos < 6; pos++)
1957 if (fgets(p, sizeof(p) - 1, msgFile) == NULL) {
1958 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1959 errorStr = "Cannot read file \'";
1960 errorStr += fileName;
1961 errorStr += "\'. Missing data.";
1962 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (missing data)\n");
1963 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
1969 ep = strrchr(p, '\r');
1971 ep = strrchr(p, '\n');
1976 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1977 errorStr = "Cannot read file \'";
1978 errorStr += fileName;
1979 errorStr += "\'. Missing data.";
1980 printfd(__FILE__, "FILES_STORE::ReadMessage - cannot read file (feof)\n");
1981 printfd(__FILE__, "FILES_STORE::ReadMessage - position: %d\n", pos);
1986 if (str2x(p, *(d[pos])))
1988 STG_LOCKER lock(&mutex, __FILE__, __LINE__);
1989 errorStr = "Cannot read file \'";
1990 errorStr += fileName;
1991 errorStr += "\'. Incorrect value. \'";
1994 printfd(__FILE__, "FILES_STORE::ReadMessage - incorrect value\n");
2001 memset(txt, 0, sizeof(txt));
2004 text->erase(text->begin(), text->end());
2005 while (!feof(msgFile))
2008 if (fgets(txt, sizeof(txt) - 1, msgFile) == NULL) {
2018 //-----------------------------------------------------------------------------
2019 int FILES_STORE::Touch(const std::string & path) const
2021 FILE * f = fopen(path.c_str(), "wb");
2029 //-----------------------------------------------------------------------------
2030 int GetFileList(std::vector<std::string> * fileList, const std::string & directory, mode_t mode, const std::string & ext)
2032 DIR * d = opendir(directory.c_str());
2036 printfd(__FILE__, "GetFileList - Failed to open dir '%s': '%s'\n", directory.c_str(), strerror(errno));
2041 while ((entry = readdir(d)))
2043 if (!(strcmp(entry->d_name, ".") && strcmp(entry->d_name, "..")))
2046 std::string str = directory + "/" + std::string(entry->d_name);
2049 if (stat(str.c_str(), &st))
2052 if (!(st.st_mode & mode)) // Filter by mode
2058 size_t d_nameLen = strlen(entry->d_name);
2059 if (d_nameLen <= ext.size())
2062 if (ext == entry->d_name + (d_nameLen - ext.size()))
2064 entry->d_name[d_nameLen - ext.size()] = 0;
2065 fileList->push_back(entry->d_name);
2070 fileList->push_back(entry->d_name);
2078 //-----------------------------------------------------------------------------