X-Git-Url: https://git.stg.codes/stg.git/blobdiff_plain/4e95741287ce4385f16eaf9d621cedd4dccbcfe3..8c6fa3fbaccc22127280bf77a48fab5a3ee0716e:/projects/stargazer/plugins/store/files/file_store.cpp diff --git a/projects/stargazer/plugins/store/files/file_store.cpp b/projects/stargazer/plugins/store/files/file_store.cpp index 689f5fab..4c95aa31 100644 --- a/projects/stargazer/plugins/store/files/file_store.cpp +++ b/projects/stargazer/plugins/store/files/file_store.cpp @@ -67,6 +67,16 @@ const int pt_mega = 1024 * 1024; namespace { PLUGIN_CREATOR fsc; + +bool CheckAndCreate(const std::string & dir, mode_t mode) +{ +if (access(dir.c_str(), F_OK) == 0) + return true; +if (mkdir(dir.c_str(), mode) == 0) + return true; +return false; +} + } extern "C" STORE * GetStore(); @@ -80,11 +90,6 @@ return fsc.GetPlugin(); //----------------------------------------------------------------------------- FILES_STORE_SETTINGS::FILES_STORE_SETTINGS() : settings(NULL), - errorStr(), - workDir(), - usersDir(), - adminsDir(), - tariffsDir(), statMode(0), statUID(0), statGID(0), @@ -105,7 +110,7 @@ PARAM_VALUE pv; pv.param = owner; std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); -if (pvi == moduleParams.end()) +if (pvi == moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'" + owner + "\' not found."; printfd(__FILE__, "%s\n", errorStr.c_str()); @@ -126,7 +131,7 @@ PARAM_VALUE pv; pv.param = group; std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); -if (pvi == moduleParams.end()) +if (pvi == moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'" + group + "\' not found."; printfd(__FILE__, "%s\n", errorStr.c_str()); @@ -164,7 +169,7 @@ PARAM_VALUE pv; pv.param = modeStr; std::vector::const_iterator pvi; pvi = find(moduleParams.begin(), moduleParams.end(), pv); -if (pvi == moduleParams.end()) +if (pvi == moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'" + modeStr + "\' not found."; printfd(__FILE__, "%s\n", errorStr.c_str()); @@ -206,7 +211,7 @@ std::vector::const_iterator pvi; PARAM_VALUE pv; pv.param = "RemoveBak"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { removeBak = true; } @@ -221,7 +226,7 @@ else pv.param = "ReadBak"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { readBak = false; } @@ -236,7 +241,7 @@ else pv.param = "WorkDir"; pvi = find(s.moduleParams.begin(), s.moduleParams.end(), pv); -if (pvi == s.moduleParams.end()) +if (pvi == s.moduleParams.end() || pvi->value.empty()) { errorStr = "Parameter \'WorkDir\' not found."; printfd(__FILE__, "Parameter 'WorkDir' not found\n"); @@ -249,8 +254,33 @@ if (workDir.size() && workDir[workDir.size() - 1] == '/') workDir.resize(workDir.size() - 1); } usersDir = workDir + "/users/"; +if (!CheckAndCreate(usersDir, GetConfModeDir())) + { + errorStr = usersDir + " doesn't exist. Failed to create."; + printfd(__FILE__, "%s\n", errorStr.c_str()); + return -1; + } tariffsDir = workDir + "/tariffs/"; +if (!CheckAndCreate(tariffsDir, GetConfModeDir())) + { + errorStr = tariffsDir + " doesn't exist. Failed to create."; + printfd(__FILE__, "%s\n", errorStr.c_str()); + return -1; + } adminsDir = workDir + "/admins/"; +if (!CheckAndCreate(adminsDir, GetConfModeDir())) + { + errorStr = adminsDir + " doesn't exist. Failed to create."; + printfd(__FILE__, "%s\n", errorStr.c_str()); + return -1; + } +servicesDir = workDir + "/services/"; +if (!CheckAndCreate(servicesDir, GetConfModeDir())) + { + errorStr = servicesDir + " doesn't exist. Failed to create."; + printfd(__FILE__, "%s\n", errorStr.c_str()); + return -1; + } return 0; } @@ -415,6 +445,24 @@ STG_LOCKER lock(&mutex); tariffList->swap(files); +return 0; +} +//----------------------------------------------------------------------------- +int FILES_STORE::GetServicesList(std::vector * list) const +{ +std::vector files; + +if (GetFileList(&files, storeSettings.GetServicesDir(), S_IFREG, ".serv")) + { + STG_LOCKER lock(&mutex); + errorStr = "Failed to open '" + storeSettings.GetServicesDir() + "': " + std::string(strerror(errno)); + return -1; + } + +STG_LOCKER lock(&mutex); + +list->swap(files); + return 0; } //----------------------------------------------------------------------------- @@ -1464,29 +1512,19 @@ if (conf.ReadString("TraffType", &str, "") < 0) return -1; } -if (!strcasecmp(str.c_str(), "up")) - td->tariffConf.traffType = TRAFF_UP; -else - if (!strcasecmp(str.c_str(), "down")) - td->tariffConf.traffType = TRAFF_DOWN; - else - if (!strcasecmp(str.c_str(), "up+down")) - td->tariffConf.traffType = TRAFF_UP_DOWN; - else - if (!strcasecmp(str.c_str(), "max")) - td->tariffConf.traffType = TRAFF_MAX; - else - { - STG_LOCKER lock(&mutex); - errorStr = "Cannot read tariff " + tariffName + ". Parameter TraffType incorrect"; - printfd(__FILE__, "FILES_STORE::RestoreTariff - invalid trafftype for tariff '%s'\n", tariffName.c_str()); - return -1; - } +td->tariffConf.traffType = TARIFF::StringToTraffType(str); if (conf.ReadString("Period", &str, "month") < 0) td->tariffConf.period = TARIFF::MONTH; else td->tariffConf.period = TARIFF::StringToPeriod(str); + +if (conf.ReadString("ChangePolicy", &str, "allow") < 0) + td->tariffConf.changePolicy = TARIFF::ALLOW; +else + td->tariffConf.changePolicy = TARIFF::StringToChangePolicy(str); + +conf.ReadTime("ChangePolicyTimeout", &td->tariffConf.changePolicyTimeout, 0); return 0; } //----------------------------------------------------------------------------- @@ -1546,25 +1584,121 @@ std::string fileName = storeSettings.GetTariffsDir() + "/" + tariffName + ".tf"; cf.WriteDouble("PassiveCost", td.tariffConf.passiveCost); cf.WriteDouble("Fee", td.tariffConf.fee); cf.WriteDouble("Free", td.tariffConf.free); + cf.WriteString("TraffType", TARIFF::TraffTypeToString(td.tariffConf.traffType)); + cf.WriteString("Period", TARIFF::PeriodToString(td.tariffConf.period)); + cf.WriteString("ChangePolicy", TARIFF::ChangePolicyToString(td.tariffConf.changePolicy)); + cf.WriteTime("ChangePolicyTimeout", td.tariffConf.changePolicyTimeout); + } + +return 0; +} +//-----------------------------------------------------------------------------*/ +int FILES_STORE::AddService(const std::string & name) const +{ +std::string fileName; +strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str()); + +if (Touch(fileName)) + { + STG_LOCKER lock(&mutex); + errorStr = "Cannot create file " + fileName; + printfd(__FILE__, "FILES_STORE::AddService - failed to add service '%s'\n", name.c_str()); + return -1; + } - switch (td.tariffConf.traffType) +return 0; +} +//-----------------------------------------------------------------------------*/ +int FILES_STORE::DelService(const std::string & name) const +{ +std::string fileName; +strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str()); +if (unlink(fileName.c_str())) + { + STG_LOCKER lock(&mutex); + errorStr = "unlink failed. Message: '"; + errorStr += strerror(errno); + errorStr += "'"; + printfd(__FILE__, "FILES_STORE::DelAdmin - unlink failed. Message: '%s'\n", strerror(errno)); + } +return 0; +} +//-----------------------------------------------------------------------------*/ +int FILES_STORE::SaveService(const SERVICE_CONF & conf) const +{ +std::string fileName; + +strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), conf.name.c_str()); + + { + CONFIGFILE cf(fileName, true); + + int e = cf.Error(); + + if (e) { - case TRAFF_UP: - cf.WriteString("TraffType", "up"); - break; - case TRAFF_DOWN: - cf.WriteString("TraffType", "down"); - break; - case TRAFF_UP_DOWN: - cf.WriteString("TraffType", "up+down"); - break; - case TRAFF_MAX: - cf.WriteString("TraffType", "max"); - break; + STG_LOCKER lock(&mutex); + errorStr = "Cannot write service " + conf.name + ". " + fileName; + printfd(__FILE__, "FILES_STORE::SaveService - failed to save service '%s'\n", conf.name.c_str()); + return -1; } - cf.WriteString("Period", TARIFF::PeriodToString(td.tariffConf.period)); + cf.WriteString("name", conf.name); + cf.WriteString("comment", conf.comment); + cf.WriteDouble("cost", conf.cost); + cf.WriteInt("pay_day", conf.payDay); + } + +return 0; +} +//----------------------------------------------------------------------------- +int FILES_STORE::RestoreService(SERVICE_CONF * conf, const std::string & name) const +{ +std::string fileName; +strprintf(&fileName, "%s/%s.serv", storeSettings.GetServicesDir().c_str(), name.c_str()); +CONFIGFILE cf(fileName); + +if (cf.Error()) + { + STG_LOCKER lock(&mutex); + errorStr = "Cannot open " + fileName; + printfd(__FILE__, "FILES_STORE::RestoreService - failed to restore service '%s'\n", name.c_str()); + return -1; + } + +if (cf.ReadString("name", &conf->name, name)) + { + STG_LOCKER lock(&mutex); + errorStr = "Error in parameter 'name'"; + printfd(__FILE__, "FILES_STORE::RestoreService - name read failed for service '%s'\n", name.c_str()); + return -1; + } + +if (cf.ReadString("comment", &conf->comment, "")) + { + STG_LOCKER lock(&mutex); + errorStr = "Error in parameter 'comment'"; + printfd(__FILE__, "FILES_STORE::RestoreService - comment read failed for service '%s'\n", name.c_str()); + return -1; + } + +if (cf.ReadDouble("cost", &conf->cost, 0.0)) + { + STG_LOCKER lock(&mutex); + errorStr = "Error in parameter 'cost'"; + printfd(__FILE__, "FILES_STORE::RestoreService - cost read failed for service '%s'\n", name.c_str()); + return -1; + } + +unsigned short value = 0; +if (cf.ReadUShortInt("pay_day", &value, 0)) + { + STG_LOCKER lock(&mutex); + errorStr = "Error in parameter 'pay_day'"; + printfd(__FILE__, "FILES_STORE::RestoreService - pay day read failed for service '%s'\n", name.c_str()); + return -1; } +conf->payDay = value; return 0; }